dhellmann / google-highly-open-participation-psf

Automatically exported from code.google.com/p/google-highly-open-participation-psf
0 stars 0 forks source link

Implement challenging rosettacode.org programming tasks with Python #131

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
RosettaCode.org is a wiki containing implementations of common
algorithms in multiple languages.  Many of the pages include Python
sample code, but some do not.  To complete this task, submit
implementations for the algorithms listed below.

- 
http://rosettacode.org/rosettacode/w/index.php?title=Data_Representation_-_Contr
olling_Fields_in_a_Structure
- http://rosettacode.org/rosettacode/w/index.php?title=Object_Serialization
- http://rosettacode.org/rosettacode/w/index.php?title=XML_and_XPath

Completion:

Edit the RosettaCode.org wiki to add a Python example to each of the 
pages listed above.  Once all of your examples are posted, add a
comment here and send an email to the ghop-python mailing list.  All
of the code samples will be tested before this task is marked as
completed, so check your work!

Other information:

Please read the instructions for contributing content to
RosettaCode.org
(http://rosettacode.org/rosettacode/w/index.php?title=Help:Contribute_Content
and
http://rosettacode.org/rosettacode/w/index.php?title=Help:Adding_a_new_programmi
ng_example)
before posting anything to the site.

Feel free to ask for help via email on the ghop-python list or
comments on this task.  The existing sample code for other languages
may not be the best possible solution for Python, but should give you
an idea of how to implement the algorithms.  All of the programming
tasks listed above can be implemented using native language operations
or standard library modules.

Task duration: please complete this task within 5 days (120 hours) of claiming 
it.

Original issue reported on code.google.com by the.good...@gmail.com on 26 Nov 2007 at 6:26

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
"I claim this task." That is, I'm assuming this task has not been claimed yet? 
Oh,
and I might need some assistance completing:
http://rosettacode.org/rosettacode/w/index.php?title=XML_and_XPath

Original comment by average....@gmail.com on 28 Nov 2007 at 7:06

GoogleCodeExporter commented 9 years ago
Yes, average.programmer, this task is yours.  Start with the parts you think 
you can
complete on your own, then let us know if you get stuck on something.

Original comment by doug.hel...@gmail.com on 28 Nov 2007 at 7:15

GoogleCodeExporter commented 9 years ago
Re the XPath, there are several XPath-capable packages out there; use Google to 
find
them.  Contact us again if you run into trouble installing them.

Note that a conclusion that "XPath doesn't work properly in any of the available
Python XML parsing packages" is sufficient for task completion -- that's 
important
information!  However, actual code that fails should be given.

thanks!

Original comment by the.good...@gmail.com on 28 Nov 2007 at 7:53

GoogleCodeExporter commented 9 years ago

Original comment by doug.hel...@gmail.com on 28 Nov 2007 at 11:58

GoogleCodeExporter commented 9 years ago
Thanks to this issue, Python has now passed Perl as the language with the most 
solved Rosetta Code tasks.  
Good job!

http://www.rosettacode.org/rosettacode/w/index.php?title=Special:Mostlinkedcateg
ories

Original comment by i...@quirkster.com on 29 Nov 2007 at 1:27

GoogleCodeExporter commented 9 years ago
I have completed the task:

-------- Task 1: 
>
http://rosettacode.org/rosettacode/w/index.php?title=Data_Representation_-_Contr
olling_Fields_in_a_Structure
>

# Controlling Fields in a Structure in Python
# This task is easily accomplished with the use of a Python dictionary.

rs232 = {
"PG   Protective ground":1,
"TD   Transmitted data":2, 
"RD   Received data":3,
"RTS  Request to send":4,
"CTS  Clear to send":5,
"DSR  Data set ready":6,
"SG   Signal ground":7,
"CD   Carrier detect":8,
"+ voltage (testing)":9,
"- voltage (testing)":10,
"SCD  Secondary CD":12,
"SCS  Secondary CTS":13,
"STD  Secondary TD":14,
"TC   Transmit clock":15,
"SRD  Secondary RD":16,
"RC   Receiver clock":17,
"SRS  Secondary RTS":19,
"DTR  Data terminal ready":20,
"SQD  Signal quality detector":21,
"RI   Ring indicator":22,
"DRS  Data rate select":23,
"XTC  External clock":24 
}

#assignation and retrieval of data is trivial

rs232["RD   Received data"] = 1
print rs232["TC   Transmit clock"]

-------- Task 2: 
> http://rosettacode.org/rosettacode/w/index.php?title=Object_Serialization

# Object Serialization in Python
# serialization in python is accomplished via the Picke module. Alternatively, 
one
can use the cPickle module if speed is the key, everything else in this example
remains the same
import pickle

class Entity:
    def __init__(self):
        self.name = "Entity"
    def printName(self):
        print self.name

class Person(Entity): #OldMan inherits from Entity
    def __init__(self): #override constructor
        self.name = "Cletus" 

instance1 = Person()
instance1.printName()

instance2 = Entity()
instance2.printName()

target = file("objects.dat", "w") # open file

# ----- Serialize
pickle.dump((instance1, instance2), target) # serialize `instance1` and 
`instance2`to
`target`
target.close() # flush file stream
print "Serialized..."

#----- Unserialize
target = file("objects.dat") # load again
i1, i2 = pickle.load(target)
print "Unserialized..."

i1.printName()
i2.printName()

-------- Task 3: 
> http://rosettacode.org/rosettacode/w/index.php?title=XML_and_XPath
# Python has basic xml parsing built in

from xml.dom import minidom

xmlfile = file("test3.xml") # load xml document from file 
xmldoc = xmldom.parse(xmlfile).documentElement # parse from file stream or...
xmldoc = xmldom.parseString("<inventory title="OmniCorp Store
#45x10^3">...</inventory>").documentElement # alternatively, parse a string

#  1st Task: Retrieve the first "item" element
i = xmldoc.getElementsByTagName("item") # get a list of all "item" tags
firstItemElement = i[0] # get the first element

# 2nd task: Perform an action on each "price" element (print it out)
for j in xmldoc.getElementsByTagName("price"): # get a list of all "price" tags
    print j.childNodes[0].data # XML Element . TextNode . data of textnode

# 3rd Task: Get an array of all the "name" elements
namesArray = xmldoc.getElementsByTagName("name")

I would also like to make a few closing comments regarding the XML example. 
I looked into pyxml v0.8.1, which seemed to be outdated, being only for Python 
2.4
(Windows)
I looked into libxml2 and tried installing in my Python 2.5 (Windows) 
Apparently it
didn't install correctly, see: http://users.skynet.be/sbi/libxml-python/

If I completed the task incorrectly, please don't hesitate to contact me so 
that I
can fix it. Also, I've been in C++ too long, this task was a great opportunity 
for me
to review Python. Thanks for the task!

Original comment by average....@gmail.com on 29 Nov 2007 at 6:09

GoogleCodeExporter commented 9 years ago
Nicely done.

Original comment by the.good...@gmail.com on 29 Nov 2007 at 9:22

GoogleCodeExporter commented 9 years ago
I expected the data structure example to use the struct module, since the 
problem as
stated on rosettacode.org is talking about interfacing with devices and uses a 
9 or
25 pin serial port as an example.  All of the other implementations use 
structures
with an appropriate number of bits.

Original comment by doug.hel...@gmail.com on 29 Nov 2007 at 1:04

GoogleCodeExporter commented 9 years ago
Hi, average.programmer, if you get a chance could you see about using the 
struct module,
http://docs.python.org/lib/module-struct.html?  The task is still complete but 
it'd
be nice to have a more comparable solution to the other languages.

Original comment by the.good...@gmail.com on 2 Dec 2007 at 8:01

GoogleCodeExporter commented 9 years ago
Hello, I'll take another close look at the problem within the next day or so. 
I've
used the struct module extensively before so it probably shouldn't be a 
problem. :)

Original comment by average....@gmail.com on 3 Dec 2007 at 7:54

GoogleCodeExporter commented 9 years ago
Hi, I had a bit of extra time to turn around and look over the quality of my
completed tasks. As for the data structure example, would the _correct_ solution
amount to packing a list of strings with struct?

Original comment by average....@gmail.com on 30 Jan 2008 at 3:26

GoogleCodeExporter commented 9 years ago
Yep.

Original comment by the.good...@gmail.com on 30 Jan 2008 at 7:03

GoogleCodeExporter commented 9 years ago
I looked at the C example, which created a simple struct with one byte 
allocated to 
each element. 
http://rosettacode.org/rosettacode/w/index.php?title=Data_Representation_-
_Controlling_Fields_in_a_Structure

So with Python struct it should be something like this?: 

import struct
x = struct.pack("9c", '0','0','0','0','0','0','0','0','0')

I dunno, but it seems like a far cry from the other examples...

Original comment by average....@gmail.com on 2 Feb 2008 at 8:30

GoogleCodeExporter commented 9 years ago
Well, the RS-232 solution as it is now does not make much sense.

What does it mean for rs232["XTC  External clock"] to have a value of 24?

I can see possibly using the structure you created, but assigning 0 to each 
field to
initialize it.

If you are going to change the example to use struct, I have no insight as to 
the
correctness of the struct code you posted. However you could certainly create a
function that allows the user to specify either field names or field numbers to 
set /
reset the bits.

I'd much rather see a Python example that is correct and useful than one that 
is short.

Original comment by miss...@hotmail.com on 9 Feb 2008 at 9:42