Skip to content

Commit

Permalink
all: Use non-u versions of built-in modules.
Browse files Browse the repository at this point in the history
This changes almost all uses of "u-module" to just "module" for the
following built-in modules:
- binascii
- collections
- errno
- io
- json
- socket
- struct
- sys
- time

There are some remaining uses of "u-module" naming, for the cases where the
built-in module is extended in Python, eg `python-stdlib/os` uses `uos`.

Also, there are remaining uses of `utime` when non-standard (compared to
CPython) functions are used, like `utime.ticks_ms()`.

Signed-off-by: Damien George <[email protected]>
  • Loading branch information
dpgeorge committed Jun 12, 2024
1 parent e1c9557 commit 816ba89
Show file tree
Hide file tree
Showing 21 changed files with 69 additions and 81 deletions.
12 changes: 6 additions & 6 deletions micropython/drivers/display/lcd160cr/lcd160cr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import machine
from utime import sleep_ms
from ustruct import calcsize, pack_into
import uerrno
import errno

# for set_orient
PORTRAIT = const(0)
Expand Down Expand Up @@ -110,7 +110,7 @@ def _waitfor(self, n, buf):
return
t -= 1
sleep_ms(1)
raise OSError(uerrno.ETIMEDOUT)
raise OSError(errno.ETIMEDOUT)

def oflush(self, n=255):
t = 5000
Expand All @@ -121,7 +121,7 @@ def oflush(self, n=255):
return
t -= 1
machine.idle()
raise OSError(uerrno.ETIMEDOUT)
raise OSError(errno.ETIMEDOUT)

def iflush(self):
t = 5000
Expand All @@ -131,7 +131,7 @@ def iflush(self):
return
t -= 1
sleep_ms(1)
raise OSError(uerrno.ETIMEDOUT)
raise OSError(errno.ETIMEDOUT)

#### MISC METHODS ####

Expand Down Expand Up @@ -254,7 +254,7 @@ def get_pixel(self, x, y):
return self.buf[3][1] | self.buf[3][2] << 8
t -= 1
sleep_ms(1)
raise OSError(uerrno.ETIMEDOUT)
raise OSError(errno.ETIMEDOUT)

def get_line(self, x, y, buf):
l = len(buf) // 2
Expand All @@ -268,7 +268,7 @@ def get_line(self, x, y, buf):
return
t -= 1
sleep_ms(1)
raise OSError(uerrno.ETIMEDOUT)
raise OSError(errno.ETIMEDOUT)

def screen_dump(self, buf, x=0, y=0, w=None, h=None):
if w is None:
Expand Down
14 changes: 7 additions & 7 deletions micropython/drivers/radio/nrf24l01/nrf24l01test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Test for nrf24l01 module. Portable between MicroPython targets."""

import usys
import ustruct as struct
import sys
import struct
import utime
from machine import Pin, SPI, SoftSPI
from nrf24l01 import NRF24L01
Expand All @@ -14,20 +14,20 @@
# initiator may be a slow device. Value tested with Pyboard, ESP32 and ESP8266.
_RESPONDER_SEND_DELAY = const(10)

if usys.platform == "pyboard":
if sys.platform == "pyboard":
spi = SPI(2) # miso : Y7, mosi : Y8, sck : Y6
cfg = {"spi": spi, "csn": "Y5", "ce": "Y4"}
elif usys.platform == "esp8266": # Hardware SPI
elif sys.platform == "esp8266": # Hardware SPI
spi = SPI(1) # miso : 12, mosi : 13, sck : 14
cfg = {"spi": spi, "csn": 4, "ce": 5}
elif usys.platform == "esp32": # Software SPI
elif sys.platform == "esp32": # Software SPI
spi = SoftSPI(sck=Pin(25), mosi=Pin(33), miso=Pin(32))
cfg = {"spi": spi, "csn": 26, "ce": 27}
elif usys.platform == "rp2": # Hardware SPI with explicit pin definitions
elif sys.platform == "rp2": # Hardware SPI with explicit pin definitions
spi = SPI(0, sck=Pin(2), mosi=Pin(3), miso=Pin(4))
cfg = {"spi": spi, "csn": 5, "ce": 6}
else:
raise ValueError("Unsupported platform {}".format(usys.platform))
raise ValueError("Unsupported platform {}".format(sys.platform))

# Addresses are in little-endian format. They correspond to big-endian
# 0xf0f0f0f0e1, 0xf0f0f0f0d2
Expand Down
17 changes: 5 additions & 12 deletions micropython/net/ntptime/ntptime.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import utime

try:
import usocket as socket
except:
import socket
try:
import ustruct as struct
except:
import struct
import time
import socket
import struct

# The NTP host can be configured at runtime by doing: ntptime.host = 'myhost.org'
host = "pool.ntp.org"
Expand Down Expand Up @@ -53,7 +46,7 @@ def time():

# Convert timestamp from NTP format to our internal format

EPOCH_YEAR = utime.gmtime(0)[0]
EPOCH_YEAR = time.gmtime(0)[0]
if EPOCH_YEAR == 2000:
# (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60
NTP_DELTA = 3155673600
Expand All @@ -71,5 +64,5 @@ def settime():
t = time()
import machine

tm = utime.gmtime(t)
tm = time.gmtime(t)
machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6] + 1, tm[3], tm[4], tm[5], 0))
14 changes: 7 additions & 7 deletions micropython/udnspkt/example_resolve.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import uio
import usocket
import io
import socket

import udnspkt


s = usocket.socket(usocket.AF_INET, usocket.SOCK_DGRAM)
dns_addr = usocket.getaddrinfo("127.0.0.1", 53)[0][-1]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
dns_addr = socket.getaddrinfo("127.0.0.1", 53)[0][-1]


def resolve(domain, is_ipv6):
buf = uio.BytesIO(48)
buf = io.BytesIO(48)
udnspkt.make_req(buf, "google.com", is_ipv6)
v = buf.getvalue()
print("query: ", v)
s.sendto(v, dns_addr)

resp = s.recv(1024)
print("resp:", resp)
buf = uio.BytesIO(resp)
buf = io.BytesIO(resp)

addr = udnspkt.parse_resp(buf, is_ipv6)
print("bin addr:", addr)
print("addr:", usocket.inet_ntop(usocket.AF_INET6 if is_ipv6 else usocket.AF_INET, addr))
print("addr:", socket.inet_ntop(socket.AF_INET6 if is_ipv6 else socket.AF_INET, addr))


resolve("google.com", False)
Expand Down
3 changes: 0 additions & 3 deletions micropython/udnspkt/udnspkt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import uio


def write_fqdn(buf, name):
parts = name.split(".")
for p in parts:
Expand Down
4 changes: 2 additions & 2 deletions micropython/umqtt.robust/umqtt/robust.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import utime
import time
from . import simple


Expand All @@ -7,7 +7,7 @@ class MQTTClient(simple.MQTTClient):
DEBUG = False

def delay(self, i):
utime.sleep(self.DELAY)
time.sleep(self.DELAY)

def log(self, in_reconnect, e):
if self.DEBUG:
Expand Down
4 changes: 2 additions & 2 deletions micropython/umqtt.simple/example_pub_button.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import time
import ubinascii
import binascii
import machine
from umqtt.simple import MQTTClient
from machine import Pin
Expand All @@ -10,7 +10,7 @@

# Default MQTT server to connect to
SERVER = "192.168.1.35"
CLIENT_ID = ubinascii.hexlify(machine.unique_id())
CLIENT_ID = binascii.hexlify(machine.unique_id())
TOPIC = b"led"


Expand Down
4 changes: 2 additions & 2 deletions micropython/umqtt.simple/example_sub_led.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from umqtt.simple import MQTTClient
from machine import Pin
import ubinascii
import binascii
import machine
import micropython

Expand All @@ -11,7 +11,7 @@

# Default MQTT server to connect to
SERVER = "192.168.1.35"
CLIENT_ID = ubinascii.hexlify(machine.unique_id())
CLIENT_ID = binascii.hexlify(machine.unique_id())
TOPIC = b"led"


Expand Down
6 changes: 3 additions & 3 deletions micropython/umqtt.simple/umqtt/simple.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import usocket as socket
import ustruct as struct
from ubinascii import hexlify
import socket
import struct
from binascii import hexlify


class MQTTException(Exception):
Expand Down
6 changes: 3 additions & 3 deletions micropython/urllib.urequest/urllib/urequest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import usocket
import socket


def urlopen(url, data=None, method="GET"):
Expand All @@ -22,10 +22,10 @@ def urlopen(url, data=None, method="GET"):
host, port = host.split(":", 1)
port = int(port)

ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM)
ai = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)
ai = ai[0]

s = usocket.socket(ai[0], ai[1], ai[2])
s = socket.socket(ai[0], ai[1], ai[2])
try:
s.connect(ai[-1])
if proto == "https:":
Expand Down
18 changes: 9 additions & 9 deletions python-ecosys/requests/requests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import usocket
import socket


class Response:
Expand Down Expand Up @@ -28,9 +28,9 @@ def text(self):
return str(self.content, self.encoding)

def json(self):
import ujson
import json

return ujson.loads(self.content)
return json.loads(self.content)


def request(
Expand All @@ -48,11 +48,11 @@ def request(
chunked_data = data and getattr(data, "__next__", None) and not getattr(data, "__len__", None)

if auth is not None:
import ubinascii
import binascii

username, password = auth
formated = b"{}:{}".format(username, password)
formated = str(ubinascii.b2a_base64(formated)[:-1], "ascii")
formated = str(binascii.b2a_base64(formated)[:-1], "ascii")
headers["Authorization"] = "Basic {}".format(formated)

try:
Expand All @@ -73,14 +73,14 @@ def request(
host, port = host.split(":", 1)
port = int(port)

ai = usocket.getaddrinfo(host, port, 0, usocket.SOCK_STREAM)
ai = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)
ai = ai[0]

resp_d = None
if parse_headers is not False:
resp_d = {}

s = usocket.socket(ai[0], usocket.SOCK_STREAM, ai[2])
s = socket.socket(ai[0], socket.SOCK_STREAM, ai[2])

if timeout is not None:
# Note: settimeout is not supported on all platforms, will raise
Expand All @@ -104,9 +104,9 @@ def request(
s.write(b"\r\n")
if json is not None:
assert data is None
import ujson
import json

data = ujson.dumps(json)
data = json.dumps(json)
s.write(b"Content-Type: application/json\r\n")
if data:
if chunked_data:
Expand Down
2 changes: 1 addition & 1 deletion python-stdlib/argparse/argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""

import sys
from ucollections import namedtuple
from collections import namedtuple


class _ArgError(BaseException):
Expand Down
6 changes: 3 additions & 3 deletions python-stdlib/binascii/test_binascii.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from binascii import *
import utime
import time

data = b"zlutoucky kun upel dabelske ody"
h = hexlify(data)
Expand All @@ -14,10 +14,10 @@

a2b_base64(b"as==") == b"j"

start = utime.time()
start = time.time()
for x in range(100000):
d = unhexlify(h)

print("100000 iterations in: " + str(utime.time() - start))
print("100000 iterations in: " + str(time.time() - start))

print("OK")
2 changes: 1 addition & 1 deletion python-stdlib/copy/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Error(Exception):
error = Error # backward compatibility

try:
from ucollections import OrderedDict
from collections import OrderedDict
except ImportError:
OrderedDict = None

Expand Down
8 changes: 4 additions & 4 deletions python-stdlib/pkg_resources/pkg_resources.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import uio
import io

c = {}

Expand All @@ -18,11 +18,11 @@ def resource_stream(package, resource):
else:
d = "."
# if d[0] != "/":
# import uos
# d = uos.getcwd() + "/" + d
# import os
# d = os.getcwd() + "/" + d
c[package] = d + "/"

p = c[package]
if isinstance(p, dict):
return uio.BytesIO(p[resource])
return io.BytesIO(p[resource])
return open(p + resource, "rb")
4 changes: 2 additions & 2 deletions unix-ffi/machine/example_timer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import utime
import time
from machine import Timer


Expand All @@ -7,5 +7,5 @@
t1.callback(lambda t: print(t, "tick1"))
t2.callback(lambda t: print(t, "tick2"))

utime.sleep(3)
time.sleep(3)
print("done")
2 changes: 0 additions & 2 deletions unix-ffi/machine/machine/timer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import ffilib
import uctypes
import array
import uos
import os
import utime
from signal import *

libc = ffilib.libc()
Expand Down
2 changes: 1 addition & 1 deletion unix-ffi/os/os/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import array
import ustruct as struct
import struct
import errno as errno_
import stat as stat_
import ffilib
Expand Down
Loading

0 comments on commit 816ba89

Please sign in to comment.