Re: dayofyear is not great when going into a new year
Den 2021-01-09 skrev Michael F. Stemper : > > A week is like a piece of string. It has two ends. > The control line of the main sheet traveler on my boat is spliced into an endless loop. http://hem.bredband.net/b262106/pages/controls/index.html I am glad work weeks are not like that :-) /Martin -- https://mail.python.org/mailman/listinfo/python-list
Re: binascii.b2a vs ord()
On 10/01/21 4:29 pm, Bischoop wrote: So what's that binascii The binascii module contains implementations of various ways of encoding *binary* data as ascii text. The one you're using there is a format called "uuencode"; it was frequently used in the days before the WWW when people often sent arbitrary files to each other by email. There's another one called "base64" which is often used behind the scenes nowadays when you attach a file to an email message. If you're trying to convert between unicode strings (all text strings are unicode in Python 3) and ascii byte strings, the binascii module is NOT what you want. Instead, you should be using the encode and decode methods of the string objects themselves: Python 3.8.2 (default, Mar 23 2020, 11:36:18) [Clang 8.1.0 (clang-802.0.42)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> s = 'Hello World' >>> s 'Hello World' >>> b = s.encode('ascii') >>> b b'Hello World' >>> s2 = b.decode('ascii') >>> s2 'Hello World' -- Greg -- https://mail.python.org/mailman/listinfo/python-list
Re: primitive password cracker
On 2021-01-08, Stefan Ram wrote: > Bischoop writes: >>What I want to learn is if I need get for example four combinations, so >>how to get in a loop first letter 'a',then another step'a' and again 'a' >>and 'a', to have '' later on'abaa' etc. > > I can only guess what you want, maybe something like > > word = [] > p = 0 > > def inc_at( position ): > word[ position ]= chr( ord( word[ position ])+ 1 ) > > while True: > if len( word )< p + 1: > word =[ "a" ]+ word > print( "".join( word )) > p = 0 > o = len( word )- 1 - p > inc_at( o ) > while p < len( word ) and word[ o ]== '{': > word[ o ]= "a" > p += 1 > o -= 1 > if p < len( word ): inc_at( o ) > > ? > > Yes, it must generate 4 characters long all combination of alphabet, equivalent to: s = list(''.join(seq) for seq initertools.product(string.ascii_lowercase, repeat=4)). I must say that's quite coding you've done. -- Thanks -- https://mail.python.org/mailman/listinfo/python-list
Pieshell: Combination of the expressiveness of shell pipelines and the power of python iterators
Hi! I've written a python package that lets you combine python code and shell pipelines: Pieshell is a Python shell environment that combines the expressiveness of shell pipelines with the prower of python iterators. It can be used in two major ways: As an interactive shell replacing e.g. bash As an ordinary python module replacing e.g. subprocess.Popen >>> list(ls("-a") | grep("-e", ".py")) ["foo.py", "bar.py"] Shell pipelines, like the one above, are ordinary Python iterator objects. Git repo: https://github.com/redhog/pieshell Installation: pip install pieshell Start the interactive shell: pieshell Hope you found this interesting, RedHog -- https://mail.python.org/mailman/listinfo/python-list
Re: binascii.b2a vs ord()
On 2021-01-10, Chris Angelico wrote: > > Trace it through, step by step. You have a series of ASCII values, > represented in binary, and then you call int() on each of them. What > sort of numbers will you get? > I'm kinda lost here about what sort of numbers I get, its class 'int'. > Then look at what chr() does when given those sorts of numbers. > > BTW, it may be helpful to look at the repr of the final string, rather > than simply printing it out. (It also may be unhelpful. If so, don't > worry about it.) > That was quite interesting text = b'This is a string' text2 = 'This is a string' res = ' '.join(format(ord(i), 'b') for i in text2) k= res.split(' ') for i in k: w= k.index(i) k[w]= int(i) s = k[0] print(type(s)) print(f'{s!r}') l = text[0] print(f'{l!r}') # output I've got: 1010100 84 -- Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: binascii.b2a vs ord()
On Mon, Jan 11, 2021 at 7:09 AM Dennis Lee Bieber wrote: > > On Sun, 10 Jan 2021 03:29:37 - (UTC), Bischoop > declaimed the following: > > >I wanted to learn about conversion string to Ascii. > >So I learn about binascii.b2a but because the output wasn't what I > >wanted got deeper and found out about ord(c) and actually that's what > >I'expected. > >So what's that binascii and why I cant convert ascii that I got from ord > >to string by using char, instead I'm getting some strings in weird > >coding. > > > > > >import binascii > >Ttext = b'This is a string' > >text2 = 'This is a string' > > Item: this is a Unicode string. Python Unicode strings are only 1-byte > per character IF all characters are in the 7-bit ASCII range. If you have > any extended characters (which would, say, be one byte in ISO-Latin-1) they > could turn the entire Unicode string into 2-byte per character (and really > expanded sets could be 3 or 4 bytes per character). > Irrelevant - they're not "one byte per character" except in terms of memory usage, which isn't coming up here. When you call ord() on a character, you get the ordinal of that character, and the internal representation doesn't matter. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: binascii.b2a vs ord()
On Mon, Jan 11, 2021 at 7:09 AM Bischoop wrote: > > On 2021-01-10, Chris Angelico wrote: > > > > Trace it through, step by step. You have a series of ASCII values, > > represented in binary, and then you call int() on each of them. What > > sort of numbers will you get? > > > > I'm kinda lost here about what sort of numbers I get, its class 'int'. Yep. Here, let me walk through one of the examples. text2 = 'This is a string' res = ' '.join(format(ord(i), 'b') for i in text2) The first character is 'T', and ord('T') is 84. Formatting that in binary gives '1010100'. c = ' '.join(chr(int(val)) for val in res.split(' ')) You then convert the string '1010100' into an integer. That doesn't give you the integer 84; it gives you the integer 1010100 (a bit over a million). Then you call chr() on that, which gives you the character with the ordinal of 1,010,100 - or in hex, U+F69B4. That's not a well-defined character (it's actually in a private-use area), so it displays as a little box. To undo the conversion from integer to binary, you have to interpret the digits as binary. You can do that with int('1010100', base=2), which will give back the integer 84. Hope that helps! ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: binascii.b2a vs ord()
On 2021-01-10 18:50, Dennis Lee Bieber wrote: On Sun, 10 Jan 2021 03:29:37 - (UTC), Bischoop declaimed the following: I wanted to learn about conversion string to Ascii. So I learn about binascii.b2a but because the output wasn't what I wanted got deeper and found out about ord(c) and actually that's what I'expected. So what's that binascii and why I cant convert ascii that I got from ord to string by using char, instead I'm getting some strings in weird coding. import binascii Ttext = b'This is a string' text2 = 'This is a string' Item: this is a Unicode string. Python Unicode strings are only 1-byte per character IF all characters are in the 7-bit ASCII range. If you have any extended characters (which would, say, be one byte in ISO-Latin-1) they could turn the entire Unicode string into 2-byte per character (and really expanded sets could be 3 or 4 bytes per character). [snip] Are you confusing the internal representation in CPython 3.3+ with UTF-8? In CPython 3.3+, Unicode strings are stored as 1 byte per codepoint if all of the codepoints are U+..U+00FF, else as 2 bytes per codepoint if all are U+01..U+10, else as 4 bytes per codepoint. But that's an implementation detail. -- https://mail.python.org/mailman/listinfo/python-list