analitics

Pages

Sunday, July 14, 2024

The Zen of Python ...

... see the The Zen of Python, with this source code import this:
python
Python 3.12.4 (tags/v3.12.4:8e8a4ba, Jun  6 2024, 19:30:16) [MSC v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Sunday, July 7, 2024

Python 3.12.1 : testing with ollama - part 001.

You need to download the ollama from the official website.
This allow you to use commands on the console: ollama --help into new command shell.
Use the pip tool to install ollma python package:
pip install ollama
Collecting ollama
  Downloading ollama-0.2.1-py3-none-any.whl.metadata (4.2 kB)
...
Installing collected packages: httpx, ollama
  Attempting uninstall: httpx
    Found existing installation: httpx 0.26.0
    Uninstalling httpx-0.26.0:
      Successfully uninstalled httpx-0.26.0
Successfully installed httpx-0.27.0 ollama-0.2.1
This command will install llama3
ollama run llama3
Let's see a basic python script with mistral model:
import ollama
from rich.console import Console
console = Console()
print(ollama.pull("mistral"))
#with console.pager(styles=True):
#	console.print(ollama.show("mistral"))
with console.pager(styles=True):
	console.print(ollama.list())
Another python script with llama3 model:
import ollama

stream = ollama.chat(
    model='llama3',
    messages=[{'role': 'user', 'content': 'Tell me the sizes of Earth?'}],
    stream=True,
)

for chunk in stream:
  print(chunk['message']['content'], end='', flush=True)
The result is this:
What a great question!

The size of Earth can be measured in various ways, depending on what aspect you're interested in. Here are some common sizes and dimensions of our planet ...

Python 3.12.1 : Check proxies on network.

Simple example about how to check proxies in network ...
import socket

# Get IP 
hostname = socket.gethostname()
workstation_ip = socket.gethostbyname(hostname)
#print("workstation_ip : ", workstation_ip )

# Put your IP's proxies from network
proxy_ips = ["192.168.1.1", "192.168.1.2", "192.168.1.3"]

# check IP proxy
if workstation_ip in proxy_ips:
    print("Is a proxy on network")
else:
    print("Not network proxy")
pip install sockschain
Collecting sockschain
  Downloading sockschain-1.0.0-py3-none-any.whl.metadata (10 kB)
...
Installing collected packages: sockschain
Successfully installed sockschain-1.0.0
Example with sockschain python module:
import socket
import sockschain as socks

# Enable debugging
def DEBUG(msg):
  print (msg)

socks.DEBUG = DEBUG
# This would have set proxies using the standard environment variables:
socks.usesystemdefaults()

# Configure a default chain
chain = [
  'tor://localhost:9050/', # First hop is Tor,
  'tor://localhost:8080/', # 8080 port,
  'ssl://proxy.example.com:8443/', # SSL to proxy.example.com
  'http://user:pass@proxy.example.com/' # ... try auth to an HTTP proxy
]
socks.setdefaultproxy() # Clear the default chain
for hop in chain:
   socks.adddefaultproxy(*socks.parseproxy(hop))

# Configure alternate routes (no proxy for localhost)
socks.setproxy('localhost', socks.PROXY_TYPE_NONE)
socks.setproxy('127.0.0.1', socks.PROXY_TYPE_NONE)
... the result is:
python socket_web.py Routes are: {'localhost': [(0, None, None, True, None, None, None)]} Routes are: {'localhost': [(0, None, None, True, None, None, None)], 'localhost.localdomain': [(0, None, None, True, None, None, None)]} Routes are: {'localhost': [(0, None, None, True, None, None, None)], 'localhost.localdomain': [(0, None, None, True, None, None, None)], '127.0.0.1': [(0, None, None, True, None, None, None)]} Routes are: {'localhost': [(0, None, None, True, None, None, None)], 'localhost.localdomain': [(0, None, None, True, None, None, None)], '127.0.0.1': [(0, None, None, True, None, None, None)], '': [(0, None, None, True, None, None, None)]} Routes are: {'localhost': [(0, None, None, True, None, None, None)], 'localhost.localdomain': [(0, None, None, True, None, None, None)], '127.0.0.1': [(0, None, None, True, None, None, None)], '': [(0, None, None, True, None, None, None)], '*': [(7, 'localhost', 9050, True, None, None, None)]} Routes are: {'localhost': [(0, None, None, True, None, None, None)], 'localhost.localdomain': [(0, None, None, True, None, None, None)], '127.0.0.1': [(0, None, None, True, None, None, None)], '': [(0, None, None, True, None, None, None)], '*': [(7, 'localhost', 9050, True, None, None, None), (7, 'localhost', 8080, True, None, None, None)]} Routes are: {'localhost': [(0, None, None, True, None, None, None)], 'localhost.localdomain': [(0, None, None, True, None, None, None)], '127.0.0.1': [(0, None, None, True, None, None, None)], '': [(0, None, None, True, None, None, None)], '*': [(7, 'localhost', 9050, True, None, None, None), (7, 'localhost', 8080, True, None, None, None), (4, 'proxy.example.com', 8443, True, None, None, ['proxy.example.com'])]} Routes are: {'localhost': [(0, None, None, True, None, None, None)], 'localhost.localdomain': [(0, None, None, True, None, None, None)], '127.0.0.1': [(0, None, None, True, None, None, None)], '': [(0, None, None, True, None, None, None)], '*': [(7, 'localhost', 9050, True, None, None, None), (7, 'localhost', 8080, True, None, None, None), (4, 'proxy.example.com', 8443, True, None, None, ['proxy.example.com']), (3, 'proxy.example.com', 8080, False, 'user', 'pass', None)]} Routes are: {'localhost': [(0, None, None, True, None, None, None)], 'localhost.localdomain': [(0, None, None, True, None, None, None)], '127.0.0.1': [(0, None, None, True, None, None, None)], '': [(0, None, None, True, None, None, None)], '*': [(7, 'localhost', 9050, True, None, None, None), (7, 'localhost', 8080, True, None, None, None), (4, 'proxy.example.com', 8443, True, None, None, ['proxy.example.com']), (3, 'proxy.example.com', 8080, False, 'user', 'pass', None)]} Routes are: {'localhost': [(0, None, None, True, None, None, None)], 'localhost.localdomain': [(0, None, None, True, None, None, None)], '127.0.0.1': [(0, None, None, True, None, None, None)], '': [(0, None, None, True, None, None, None)], '*': [(7, 'localhost', 9050, True, None, None, None), (7, 'localhost', 8080, True, None, None, None), (4, 'proxy.example.com', 8443, True, None, None, ['proxy.example.com']), (3, 'proxy.example.com', 8080, False, 'user', 'pass', None)]}

Tuesday, July 2, 2024

Python 3.12.1 : Ursina python game engine - part 002 .

This is the second tutorial with ursina, because now I use Python version 3.12.1 and Ursina comes with version 7.0.0 .
The install is easy with the pip tool.
pip install ursina
Collecting ursina
...
Installing collected packages: panda3d, screeninfo, panda3d-simplepbr, panda3d-gltf, ursina
Successfully installed panda3d-1.10.14 panda3d-gltf-1.2.0 panda3d-simplepbr-0.12.0 screeninfo-0.8.1 ursina-7.0.0
About Ursina you can read more on the official website.
Platforms:
  • Windows
  • Linux
  • Mac (not officially supported, but will most likely work)
I tested with samples from the GitHub project and works well.
from ursina import *

app = Ursina(size=(1280,720))

physics_entities = []
class PhysicsEntity(Entity):
    def __init__(self, model='cube', collider='box', **kwargs):
        super().__init__(model=model, collider=collider, **kwargs)
        physics_entities.append(self)

    def update(self):
        if self.intersects():
            self.stop()
            return

        self.velocity = lerp(self.velocity, Vec3(0), time.dt)
        self.velocity += Vec3(0,-1,0) * time.dt * 5
        self.position += (self.velocity + Vec3(0,-4,0)) * time.dt


    def stop(self):
        self.velocity = Vec3(0,0,0)
        if self in physics_entities:
            physics_entities.remove(self)

    def on_destroy(self):
        self.stop()


    def throw(self, direction, force):
        pass

from ursina.shaders import lit_with_shadows_shader
Entity.default_shader = lit_with_shadows_shader
DirectionalLight().look_at(Vec3(1,-1,-1))

ground = Entity(model='plane', scale=32, texture='white_cube', texture_scale=Vec2(32), collider='box')

from ursina.prefabs.first_person_controller import FirstPersonController
player = FirstPersonController()

def input(key):
    if key == 'left mouse down':
        e = PhysicsEntity(model='cube', color=color.azure, velocity=Vec3(0), position=player.position+Vec3(0,1.5,0)+player.forward, collider='sphere')
        e.velocity = (camera.forward + Vec3(0,.5,0)) * 10
        # physics_entities.append(e)

Sky()
app.run()