help adding string module to mu or any other ide

Questions and discussion about running MicroPython on a micro:bit board.
Target audience: MicroPython users with a micro:bit.
Post Reply
JKD
Posts: 4
Joined: Mon Mar 27, 2017 1:06 am

help adding string module to mu or any other ide

Post by JKD » Mon Mar 27, 2017 1:14 am

Hi

New member & new micro Python user here

I am developing a microbit project which needs the zfill function from the string module.

How do I add this module to mu or any other IDE?

Any help would be appreciated

Thanks

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: help adding string module to mu or any other ide

Post by deshipu » Mon Mar 27, 2017 7:37 am

Can't you just implement that function in Python yourself?

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: help adding string module to mu or any other ide

Post by Roberthh » Mon Mar 27, 2017 12:53 pm

Just in case you can't, here is a try:

Code: Select all

def zfill(s, width):
    if len(s) < width:
        return ("0" * (width - len(s))) + s
    else:
        return s
The difference to the built-in method: instead of

Code: Select all

s.zfill(width)
you have to call

Code: Select all

zfill(s, width)
And yes, this function may consume more heap space than a built-in method.

JKD
Posts: 4
Joined: Mon Mar 27, 2017 1:06 am

Re: help adding string module to mu or any other ide

Post by JKD » Mon Mar 27, 2017 3:46 pm

Many thanks to you both

Robert I am certainly NOT able to , am getting back into a little programming after a long break. Last programming I did was on a commodore pet & zx81!!!
Can't wait to get home and try your code , looks like it will do just what I need

Regards

JKD

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: help adding string module to mu or any other ide

Post by pythoncoder » Tue Mar 28, 2017 7:33 am

An alternative (more efficient) approach is:

Code: Select all

def zfill(s, width):
	return '{:0>{w}}'.format(s, w=width)
Peter Hinch
Index to my micropython libraries.

JKD
Posts: 4
Joined: Mon Mar 27, 2017 1:06 am

Re: help adding string module to mu or any other ide

Post by JKD » Tue Mar 28, 2017 7:43 am

Hi peter

Many thanks

User avatar
TravisT
Posts: 72
Joined: Sun Feb 23, 2014 2:31 pm
Location: Portland, OR
Contact:

Re: help adding string module to mu or any other ide

Post by TravisT » Tue Apr 04, 2017 3:00 am

My two cents is it would be nice to use zfill the same way as in the standard python library, especially when using micropython unix.

I also know if I am complaining that I should contribute to the project and add it myself :-)
_______________
Travis Travelstead

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: help adding string module to mu or any other ide

Post by deshipu » Tue Apr 04, 2017 6:46 am

There is a reason why it's called MicroPython.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: help adding string module to mu or any other ide

Post by pythoncoder » Wed Apr 05, 2017 8:20 am

Indeed. New users are advised to read this https://github.com/micropython/micropyt ... ifferences.
Peter Hinch
Index to my micropython libraries.

JKD
Posts: 4
Joined: Mon Mar 27, 2017 1:06 am

Re: help adding string module to mu or any other ide

Post by JKD » Wed Apr 05, 2017 9:17 am

Many thanks to all the kind & helpful people who have helped with advice & code.

Really appreciate all the time & trouble you went to !

Regards

Jason

Post Reply