Re: [Tutor] Help understanding base64 decoding

2018-09-13 Thread Ryan Smith
Hi Peter,

Thank you for the explanation! I have been banging my head around this
for almost two days. I'm still getting familiar with all of the
different encodings at play. For example the way I currently
understand things is that python supports unicode which ultimately
defaults to being encoded in UTF-8. Hence I'm guessing is  the reason
for converting strings to a bytes object in the first place. Again
thank you for the assistance!

Ryan

On Thu, Sep 13, 2018 at 2:57 AM, Peter Otten <__pete...@web.de> wrote:
> Ryan Smith wrote:
>
>> Hello All,
>>
>> I am currently working on a small utility that finds any base64
>> encoded strings in files and decodes them. I am having issue
>> understanding how the Base64 module actually works. The regular
>> expression that I am using correctly matches on the encoded strings. I
>> simply want to be able to convert the match of the encoded ascii
>> string to it's decoded ascii equivalent. For example the base64
>> encoded ascii string 'UwB5AHMAdABlAG0ALgBkAGwAbAA=' will decode to
>> 'System.dll' if I use an online base64 decoder. However I get a
>> completely different output when trying to codify this using python
>> 3.6.5:
>>
>>>>>import base64
>>>>>import binascii
>>
>>>>>test_str = 'UwB5AHMAdABlAG0ALgBkAGwAbAA='
>>>>> base64.b64decode(test_str)
>> b'S\x00y\x00s\x00t\x00e\x00m\x00.\x00d\x00l\x00l\x00'
>>
>>>>>temp = base64.b64decode(test_str)
>>>>>binascii.b2a_base64(temp)
>> b'UwB5AHMAdABlAG0ALgBkAGwAbAA=\n'
>>
>> I understand that when decoding and encoding you have to use bytes
>> objects but what I don't understand is why I can't get the proper
>> conversion of the original ascii string. Can someone please point me
>> in the right direction?
>
> Look closely at the odd bytes in
>
>> b'S\x00y\x00s\x00t\x00e\x00m\x00.\x00d\x00l\x00l\x00'
>
> or just do
>
>>>> b'S\x00y\x00s\x00t\x00e\x00m\x00.\x00d\x00l\x00l\x00'[::2]
> b'System.dll'
>
> The even bytes are all NUL:
>
>>>> b'S\x00y\x00s\x00t\x00e\x00m\x00.\x00d\x00l\x00l\x00'[1::2]
> b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
>
> This means that your byte string already *is* the original string, encoded
> as UTF-16. You can convert it into a string with
>
>>>> b'S\x00y\x00s\x00t\x00e\x00m\x00.\x00d\x00l\x00l\x00'.decode("utf-16")
> 'System.dll'
>
> which will handle non-ascii characters correctly, too.
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Help understanding base64 decoding

2018-09-12 Thread Ryan Smith
Hello All,

I am currently working on a small utility that finds any base64
encoded strings in files and decodes them. I am having issue
understanding how the Base64 module actually works. The regular
expression that I am using correctly matches on the encoded strings. I
simply want to be able to convert the match of the encoded ascii
string to it's decoded ascii equivalent. For example the base64
encoded ascii string 'UwB5AHMAdABlAG0ALgBkAGwAbAA=' will decode to
'System.dll' if I use an online base64 decoder. However I get a
completely different output when trying to codify this using python
3.6.5:

>>>import base64
>>>import binascii

>>>test_str = 'UwB5AHMAdABlAG0ALgBkAGwAbAA='
>>> base64.b64decode(test_str)
b'S\x00y\x00s\x00t\x00e\x00m\x00.\x00d\x00l\x00l\x00'

>>>temp = base64.b64decode(test_str)
>>>binascii.b2a_base64(temp)
b'UwB5AHMAdABlAG0ALgBkAGwAbAA=\n'

I understand that when decoding and encoding you have to use bytes
objects but what I don't understand is why I can't get the proper
conversion of the original ascii string. Can someone please point me
in the right direction?

Thank you,

Ryan
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Coming from R, what's a good IDE editor? I've tried PyCharm and Spyder

2017-06-05 Thread Ryan Smith
+1 for Wing IDE. I have been using it for about 6-7 months now and
absolutely love it.

Ryan
On Sun, Jun 4, 2017 at 11:37 AM Ryan Smith  wrote:

>
> On Sun, Jun 4, 2017 at 8:13 AM wolfrage8...@gmail.com <
> wolfrage8...@gmail.com> wrote:
>
>> Atom.io Editor is my current favorite after having swapped around a lot.
>>
>> http://www.marinamele.com/install-and-configure-atom-editor-for-python
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] feedback on simple python code

2017-03-02 Thread Ryan Smith
On 2/28/17, 3:32 AM, "Tutor on behalf of Peter Otten"

wrote:

>Ryan Smith wrote:
>
>> Hi all,
>> 
>> New python student here. I have been using O¹reilly¹s "Python Beyond the
>> Basics: Object Oriented Programming video series". In one of the
>> assignments we are to write a simple inheritance hierarchy of three
>> classes that write to text files. I have actually written the code for
>>the
>> assignment and it runs as well as meets the requirements of the
>> assignment. I am posting to get feedback on how I can improve this and
>> making it more pythonic and concise.
>> 
>> Please keep in mind that I am simply ³simulating" writing to a log file
>> and a tabbed delimited file, so I¹m not necessarily looking for which
>> modules I could have to create actual log files or writing to actual csv
>> files. The output of the code should be two text files with text written
>> to them that have been passed to the instance of each respective object.
>> 
>> #!/usr/bin/env python
>> 
>> import abc
>> import datetime
>> 
>> class WriteFile(object):
>> __metaclass__ = abc.ABCMeta
>> 
>> 
>> @abc.abstractmethod
>> def write(self,text):
>
>You might run a tool like https://pypi.python.org/pypi/pycodestyle over
>your 
>code to ensure it follows common standards.
>
>> """Write to file"""
>> return
>> 
>> 
>> class LogFile(WriteFile):
>> def __init__(self,fh):
>> self.fh = fh
>> 
>> 
>> def write(self, text):
>> date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
>> entry = "{0} {1}{2}".format(date, text, '\n')
>> with open(self.fh, 'a') as f:
>> f.write(entry)
>> 
>> class DelimWrite(WriteFile):
>> def __init__(self, fh, delim):
>> self.fh = fh
>> self.delim = delim
>> 
>> 
>> def write(self, text):
>> with open(self.fh, 'a') as f:
>> entry = ""
>> for item in text:
>> if self.delim in item:
>> entry += ' "{0}"{1} '.format(item,self.delim)
>
>What will happen if text contains double quotes?
>
>> else:
>> entry += item+self.delim
>
>Have a look at the str.join() method. Example:
>
>>>> ",".join(["foo", "bar", "baz"])
>'foo,bar,baz'
>
>> f.write(entry.strip(self.delim) + '\n')
>
>I will mention another "principle", DRY (don't repeat yourself), i. e. do
>not write the same code twice.
>
>When I look at your code I see that both DelimWrite and LogFile open a
>file. 
>If you want modify your code to accept file objects like sys.stdout you
>have 
>to make changes in both subclasses. To avoid that you might put the file-
>writing part into a separate method:
>
>class WriteFile(object):
>def __init__(self, file):
>self.file = file
>
>def write_record(self, record):
>with open(self.file, "a") as f:
>f.write(self.format_record(record))
>
>def format_record(self, record):
>return record
>
>
>class LogFile(WriteFile):
>def format_record(self, record):
>date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
>return "{0} {1}\n".format(date, record)
>
>
>class DelimWrite(WriteFile):
>quote = '"'
>def __init__(self, file, delimiter):
>super(DelimWrite, self).__init__(file)
>self.delimiter = delimiter
>
>def escaped(self, value):
>value = str(value)
>if self.delimiter in value:
>quote = self.quote
>value = '"{}"'.format(value.replace(quote, quote + quote))
>return value
>
>def format_record(self, record):
>escaped_rows = (self.escaped(value) for value in record)
>return self.delimiter.join(escaped_rows) + "\n"
>
>
>___
>Tutor maillist  -  Tutor@python.org
>To unsubscribe or change subscription options:
>https://mail.python.org/mailman/listinfo/tutor
>

Danny/Peter,

Thank you to both of you for your feedback.

Peter,

I will definitely be adding pycodestyle to my toolkit. You made a good
point about creating a separate method for file writing so that both the
DelimWrite and the LogFile classes can use them. Also looking at my code I
see where my code does repeat with the write() method. I guess I’m still
trying to understand polymorphism because that’s what I was aiming for
when I wrote the code.

Danny,

Thank you for the link you provided on Liskov’s principals. To be honest I
started reading what you shared and realize it’s gonna take a little time
to digest it so that I can apply the principals in future code. I’m sure I
will have more questions, but I will just start a separate thread.

Again thank you both,

Ryan

 


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] feedback on simple python code

2017-02-27 Thread Ryan Smith
Hi all,

New python student here. I have been using O¹reilly¹s "Python Beyond the
Basics: Object Oriented Programming video series". In one of the
assignments we are to write a simple inheritance hierarchy of three
classes that write to text files. I have actually written the code for the
assignment and it runs as well as meets the requirements of the
assignment. I am posting to get feedback on how I can improve this and
making it more pythonic and concise.

Please keep in mind that I am simply ³simulating" writing to a log file
and a tabbed delimited file, so I¹m not necessarily looking for which
modules I could have to create actual log files or writing to actual csv
files. The output of the code should be two text files with text written
to them that have been passed to the instance of each respective object.

#!/usr/bin/env python

import abc
import datetime

class WriteFile(object):
__metaclass__ = abc.ABCMeta


@abc.abstractmethod
def write(self,text):
"""Write to file"""
return 


class LogFile(WriteFile):
def __init__(self,fh):
self.fh = fh


def write(self, text):
date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
entry = "{0} {1}{2}".format(date, text, '\n')
with open(self.fh, 'a') as f:
f.write(entry)

class DelimWrite(WriteFile):
def __init__(self, fh, delim):
self.fh = fh
self.delim = delim


def write(self, text):
with open(self.fh, 'a') as f:
entry = ""
for item in text:
if self.delim in item:
entry += ' "{0}"{1} '.format(item,self.delim)
else:
entry += item+self.delim
f.write(entry.strip(self.delim) + '\n')




if __name__ == "__main__":


log = LogFile('log.txt')
log.write('This is a log message')
log.write('This is another log message')


d = DelimWrite('test.csv',',')
d.write([Œ1¹,¹2¹,¹3¹])
d.write(['a','this, that','c¹])#Double quote item if delimiter
included as list item
d.write([Œbasketball¹,'football¹,'golfball'])




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need help

2016-10-13 Thread Ryan Smith
On Wednesday, October 12, 2016, Alan Gauld via Tutor 
wrote:

> On 12/10/16 09:03, niraj pandey wrote:
>
> > Can you pls guide how to print this screen (Attached here) content in
> > printer ?
>
> As we already pointed out this is a text list so attachments
> are usually stripped off...
>
> However, printing from Tkinter is not easy. The simplest way is usually
> to create an HTML file and use the OS to print that via a browser (many
> browsers have a print command line option). It is possible to convert
> your screen into a graphics file and print that, but the results can be
> a bit unpredictable.
>
> Alan G.
>
> ___
> Tutor maillist  -  Tutor@python.org 
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Why define a function inside a function?

2016-01-30 Thread Ryan Smith
Thank you to both you and Ben for taking the time and answering my
question! The detailed explanation of the example code I provided
really helped and made things clear. As did the explanations about
closures and a simplified example. I definitely have a lot to learn,
but this was very educational nonetheless.





On Fri, Jan 29, 2016 at 10:38 PM, Steven D'Aprano  wrote:
> Oops, I screwed up!
>
>
> On Sat, Jan 30, 2016 at 02:31:09PM +1100, Steven D'Aprano wrote:
>
>> Complicated? Well, a bit. Here's a simpler demonstration of a closure:
>>
>>
>> def factory(num):
>> """Factory function that returns a new function that adds
>> num to whatever it is given."""
>> def adder(x):
>> return x + num
>
>
> Of course that can't work, because I forgot to return the inner
> function.
>
> def factory(num):
> def adder(x):
> return x + num
> return adder
>
>
> will work better. Sorry for any confusion.
>
>
>
> --
> Steve
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Why define a function inside a function?

2016-01-29 Thread Ryan Smith
Hi all,

I am new to programming and python and had a question. I hope I
articulate this well enough so here it goes... I was following along
on a thread on this mailing list discussing how to test file I/O
operations. In one of the suggested solutions the following code was
used:


def _open_as_stringio(self, filename):
filelike = StringIO(self.filecontents[filename])
real_close = filelike.close
def wrapped_close():
self.filecontents[filename] = filelike.getvalue()
real_close()
filelike.close = wrapped_close
return filelike

In trying to understand the logic behind the code for my own
edification, I was wondering what is the reasoning of defining a
function in side another function (for lack of a better phrase)?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor