[Tutor] Help!

2018-04-19 Thread Joshua Nghe
Hi,
This is Joshua N from Campus Middle School. In my science class, we are 
learning topics of our choice. I chose coding as my project, and I am 
contacting you to ask about certain projects which would be appropriate for a 
programmer at a beginner level. I only have 15 hours on Python, however I 
expect to get to at least double that when I start to work on my project. What 
projects would be appropriate for someone of my level.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] beginning encryption

2018-04-19 Thread Roger Lea Scherer
I am absolutely stumped. I've tried a number of different scenarios and
copied the answer more than I like, but I still can't figure this out. I
don't want to copy the answer verbatim because then I won't learn.

I'm doing the beginning cipher, mix up the letters routine. I get the
entire Gettysburg address with no alterations in this form of the code (and
a few others I've tried). I do not receive any error, but I expect the
character in the Gettysburg address to change to the index position in the
encryption variable.

What am I not getting?

Thank you as always.



address = """Four score and seven years ago our fathers brought forth on
this continent, a new nation,
conceived in Liberty, and dedicated to the proposition that all men are
created equal.
Now we are engaged in a great civil war, testing whether that nation, or
any nation so conceived
and so dedicated, can long endure. We are met on a great battle-field of
that war. We have come
to dedicate a portion of that field, as a final resting place for those who
here gave their lives that
that nation might live. It is altogether fitting and proper that we should
do this.
But, in a larger sense, we can not dedicate -- we can not consecrate -- we
can not hallow -- this ground.
The brave men, living and dead, who struggled here, have consecrated it,
far above our poor power
to add or detract. The world will little note, nor long remember what we
say here, but it can never
forget what they did here. It is for us the living, rather, to be dedicated
here to the unfinished work
which they who fought here have thus far so nobly advanced. It is rather
for us to be here dedicated
to the great task remaining before us -- that from these honored dead we
take increased devotion
to that cause for which they gave the last full measure of devotion -- that
we here highly resolve
that these dead shall not have died in vain -- that this nation, under God,
shall have a new birth
of freedom -- and that government of the people, by the people, for the
people, shall not perish
from the earth."""

alphabet = "abcdefghijklmnopqrstuvwxyz"
encryption = "nopqrstuvwxyzabcdefghijklm"


def encryptor(address):
encrypted = ""
for char in address:
if char != alphabet:
encrypted += char
else:
pos = alphabet.index(char)
encrypted += encryption[pos]
print(encrypted)

encryptor(address)


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


[Tutor] Beginner Level Projects

2018-04-19 Thread Joshua Nghe
Hi,

This is Joshua N from Campus Middle School in CO. Our science class is 
collectively participating in a project that consumes 20% of our classtime 
every week. For my project, I chose to learn Python, and create something from 
what I learned. I currently have around 10 hours spent learning Python, and 
predict to have at least 20 by the time I start my project. According to my 
level of experience, what would be an interesting project that would exihbit 
all that I've learned.


Thanks,

   Joshua

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


[Tutor] File handling Tab separated files

2018-04-19 Thread Niharika Jakhar
Hi
I want to store a file from BioGRID database (tab separated file, big data)
into a data structure(I prefer lists, please let me know if another would
be better) and I am trying to print the objects.
Here’s my code:
class BioGRIDReader:
def __init__(self, filename):
with open('filename', 'r') as file_:
read_data = f.read()
for i in file_ :
read_data = (i.split('\t'))
return (objects[:100])

a = BioGRIDReader
print (a.__init__(test_biogrid.txt))




Here's what the terminal says:
Traceback (most recent call last):
  File "./BioGRIDReader.py", line 23, in 
print (a.__init__(test_biogrid.txt))
NameError: name 'test_biogrid' is not defined

The file named test_biogrid.txt do exist in the same folder as this program.

I am unable to go further with this code. Kindly help me out.


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


Re: [Tutor] beginning encryption

2018-04-19 Thread Steven D'Aprano
Hi Roger, and welcome. See my comments below.


On Wed, Apr 18, 2018 at 04:39:27PM -0700, Roger Lea Scherer wrote:

> def encryptor(address):
> encrypted = ""
> for char in address:
> if char != alphabet:
> encrypted += char
> else:
> pos = alphabet.index(char)
> encrypted += encryption[pos]
> print(encrypted)

For each character in the Gettysburg address, you compare the individual 
single character "F", "o", "u", "r", etc against the ENTIRE alphabet 
"abcde...xyz".

Since a single letter is never equal to 26 letters, you always get

char != alphabet  # they are never equal

and so you always add the char unchanged to the encrypted text.

Try this instead:

if char not in alphabet:
encrypted += char

Then once you have that working, write back (with a shorter extract of 
the Gettysburgh address please!) and we'll see how else we can improve 
your code.



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


Re: [Tutor] Help!

2018-04-19 Thread Steven D'Aprano
Hello Joshua, and welcome!

My comments below.


On Thu, Apr 19, 2018 at 01:15:59AM +, Joshua Nghe wrote:
> Hi,
> This is Joshua N from Campus Middle School.

You are talking to people from all over the world, and some of us are 
not familiar with what you mean by "Middle School". What is it? About 
what age group are you in?


> In my science class, we 
> are learning topics of our choice. I chose coding as my project, and I 
> am contacting you to ask about certain projects which would be 
> appropriate for a programmer at a beginner level. I only have 15 hours 
> on Python, however I expect to get to at least double that when I 
> start to work on my project. What projects would be appropriate for 
> someone of my level.

Very simple projects! 30 hours is not a lot of time to learn a 
programming language if your aim is a complex project.

Python is a great language and I love it, but unfortunately the graphics 
capabilities are very primitive, and even when you can get them working, 
it might take many more hours to become productive with them. So unless 
you have a LOT of help from your class, I recommand you stick to a 
text-based program.

Does your project have to be oriented towards science, or can it be 
anything at all?

Some program ideas...

Encrypt and decrypt text using a Caesar Cipher.

https://en.wikipedia.org/wiki/Caesar_cipher

or one of many other simple ciphers.

Calculate the prime numbers up to a certain limit.

https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

Play "Guess the number" games.

Play 20 Questions.

(Advanced)
Given the formula for a (simple) molecule like H20, print the names of 
the elements in the molecule, their atomic weights, and the total 
molecular weight.

Convert between temperature scales (degrees Celsius, Fahrenheit, and 
Kelvin).



Try reading here:

http://www.programmingforbeginnersbook.com/blog/what_should_i_make_beginner_programming_project_ideas/

Hope this helps!



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


Re: [Tutor] File handling Tab separated files

2018-04-19 Thread Steven D'Aprano
On Thu, Apr 19, 2018 at 10:45:07AM +0200, Niharika Jakhar wrote:
> Hi
> I want to store a file from BioGRID database (tab separated file, big data)
> into a data structure(I prefer lists, please let me know if another would
> be better) and I am trying to print the objects.

You should probably look at using the csv module. It can handle 
tab-separated files too.

https://docs.python.org/3/library/csv.html

https://docs.python.org/2/library/csv.html


> Here’s my code:
> class BioGRIDReader:
> def __init__(self, filename):
> with open('filename', 'r') as file_:
> read_data = f.read()
> for i in file_ :
> read_data = (i.split('\t'))
> return (objects[:100])
> 
> a = BioGRIDReader
> print (a.__init__(test_biogrid.txt))

You have two errors here. Firstly, you should not call a.__init__ 
directly. You never call methods with two leading and trailing 
underscores directly: always let Python call them for you.

Instead, you call:

a = BioGRIDReader(filename)

Your second error is the file name. You write:

test_biogrid.txt

but to Python, that looks like a variable called test_biogrid, which 
does not exist. That is why you get the error:

> Here's what the terminal says:
> Traceback (most recent call last):
>   File "./BioGRIDReader.py", line 23, in 
> print (a.__init__(test_biogrid.txt))
> NameError: name 'test_biogrid' is not defined

The solution is to quote the name of the file, so Python treats it as a 
string:

a = BioGRIDReader("test_biogrid.txt")

By the way, THANK YOU for quoting the whole error message! That is much 
better than people who just say "it doesn't work" and leave us to guess 
what happens.




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


Re: [Tutor] File handling Tab separated files

2018-04-19 Thread Wolfgang Maier

On 04/19/2018 10:45 AM, Niharika Jakhar wrote:

Hi
I want to store a file from BioGRID database (tab separated file, big data)
into a data structure(I prefer lists, please let me know if another would
be better) and I am trying to print the objects.
Here’s my code:
class BioGRIDReader:
 def __init__(self, filename):
 with open('filename', 'r') as file_:
 read_data = f.read()
 for i in file_ :
 read_data = (i.split('\t'))
 return (objects[:100])

a = BioGRIDReader
print (a.__init__(test_biogrid.txt))



In addition to your immediate problem, which Steven explained already, 
you will run into more issues with the posted code:


1) in your open() call you have filename quoted
This is kind of the opposite of the mistake Steven points out.
Here, filename really is an identifier known to Python, but by quoting 
it it, you will always try to open a file literally named 'filename'


2) wrong indentation after the "with open( ..." line
with starts a block of indented code, in which you will have access to 
the opened file


3) file_ is the identifier under which you will have access to the input 
file's content, but on the very next line you're trying to use f.read().

f won't have any meaning for Python at that point

4) Even if you used file_.read() at that step, it would be wrong because 
in the subsequent for loop you are trying to consume file_ line by line.
However, if you read() all of file_ before, there won't be anything left 
to loop over.


5) You are reading the data into a list called read_data, but you are 
trying to return a slice of an identifier objects, which Python will not 
know about when it gets there


6) As Steven said, you shouldn't call __init__() directly,
but that also means that you should not return data from it.
Instead you might want to only parse the file contents in the __init__ 
method and store that data as an attribute in self (e.g., use self.data 
for this). Then you could use things like this:


a = BioGRIDReader('test_biogrid.txt')
print(a.data[:100])

Best,
Wolfgang


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


Re: [Tutor] XML Programs

2018-04-19 Thread Glen
Hey guys,

I have the following code:

https://repl.it/@glendog/HurtfulPunctualInterface

Using the function I have define I can print to screen a list of books.
However, how can I search for records within the xml using an ID or the
title of the book etc? I've tried reading the tutorial but the penny is not
dropping.

On 18 April 2018 at 08:39, Glen  wrote:

> Hello Stefan,
>
> Thank you for this. That's actually quite helpful!
>
> Regards,
>
> On 17 April 2018 at 19:56, Stefan Behnel  wrote:
>
>> Glen schrieb am 16.04.2018 um 13:10:
>> > I'm writing a save-game editor for a game I play (just a project to
>> learn).
>> > But I am struggling on how to structure the code, how to store the xml
>> data
>> > in data structure etc,
>> >
>> > Can anyone recommend some source I can review that reads and writes data
>> > from an xml file.
>>
>> Here's a tutorial for the lxml package:
>>
>> http://lxml.de/tutorial.html
>>
>> However, I'd first check if there really is no Python library yet that
>> handles your "game files", whatever format they may have. One of the most
>> important things to learn about software engineering is to know when *not*
>> to write code to solve a problem.
>>
>> If you end up having (or wanting) to deal with the bare XML format
>> yourself, you may consider implementing your own XML API for your format,
>> so that you can nicely assign functionality to certain tags in the
>> document
>> tree. See the section on "Implementing Namespaces" here:
>>
>> http://lxml.de/element_classes.html
>>
>> Stefan
>>
>> ___
>> 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] File handling Tab separated files

2018-04-19 Thread Mats Wichmann
On 04/19/2018 07:57 AM, Wolfgang Maier wrote:
> On 04/19/2018 10:45 AM, Niharika Jakhar wrote:
>> Hi
>> I want to store a file from BioGRID database (tab separated file, big
>> data)
>> into a data structure(I prefer lists, please let me know if another would
>> be better) and I am trying to print the objects.
>> Here’s my code:
>> class BioGRIDReader:
>>  def __init__(self, filename):
>>  with open('filename', 'r') as file_:
>>  read_data = f.read()
>>  for i in file_ :
>>  read_data = (i.split('\t'))
>>  return (objects[:100])
>>
>> a = BioGRIDReader
>> print (a.__init__(test_biogrid.txt))
>>
> 
> In addition to your immediate problem, which Steven explained already,
> you will run into more issues with the posted code:

In addition to this low level advice, let me observe that whenever the
term "big data" is tossed into the discussion, you want to consider
whether reading it all in to Python's memory into a "simple" data
structure in one go is what you want to do.  You may want to look into
the Pandas project (possibly after spending a little more time becoming
comfortable with Python itself first):

https://pandas.pydata.org/

Pandas has its own file handling code (particularly, a read_csv
function) which might end up being useful.


Also quite by chance, I happen to know there's an existing project to
interact with the BioGRID web service, have no idea if that would be a
match for any of your needs.  A quick google to refind it:

https://github.com/arvkevi/biogridpy

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


Re: [Tutor] Beginner Level Projects

2018-04-19 Thread William Ray Wing

> On Apr 18, 2018, at 9:34 PM, Joshua Nghe  wrote:
> 
> Hi,
> 
> This is Joshua N from Campus Middle School in CO. Our science class is 
> collectively participating in a project that consumes 20% of our classtime 
> every week. For my project, I chose to learn Python, and create something 
> from what I learned. I currently have around 10 hours spent learning Python, 
> and predict to have at least 20 by the time I start my project. According to 
> my level of experience, what would be an interesting project that would 
> exihbit all that I've learned.
> 

Since we don’t know your interests, or how fast your are progressing, it is a 
bit hard to make suggestions.  But if you were to Google “simple python 
projects” you will get a page full of sites with lists and suggestions of fun 
starter projects.

Bill

> 
> Thanks,
> 
>   Joshua
> 
> ___
> 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] Beginner Level Projects

2018-04-19 Thread David Rock

> On Apr 18, 2018, at 20:34, Joshua Nghe  wrote:
> 
> Hi,
> 
> This is Joshua N from Campus Middle School in CO. Our science class is 
> collectively participating in a project that consumes 20% of our classtime 
> every week. For my project, I chose to learn Python, and create something 
> from what I learned. I currently have around 10 hours spent learning Python, 
> and predict to have at least 20 by the time I start my project. According to 
> my level of experience, what would be an interesting project that would 
> exihbit all that I've learned.

Start by thinking about problems you would like to solve.  Is there something 
that you wish you could do easier?

Are you in 6th grade, or 8th?  What other kinds of classes are you in where 
python might help?  For example, have you done any graphing in a Math class?  
making a graph of a function using text is a pretty complete exercise that 
might help you visualize problems (and help with you other classes, too).

Are you interested in making things move or turning stuff on/off?  Using python 
on a Raspberry Pi is a great way to start with using a computer to control the 
“real world.”


— 
David Rock
da...@graniteweb.com




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


Re: [Tutor] XML Programs

2018-04-19 Thread Peter Otten
Glen wrote:

> Hey guys,
> 
> I have the following code:
> 
> https://repl.it/@glendog/HurtfulPunctualInterface

from lxml import etree
catalog = etree.parse("example.xml")


def getbooks(xmldata):
books = xmldata.xpath("//catalog")[0]
for item in books:
print(item.findtext("title"))

getbooks(catalog)

> Using the function I have define I can print to screen a list of books.
> However, how can I search for records within the xml using an ID or the
> title of the book etc? I've tried reading the tutorial but the penny is
> not dropping.

As a rule of thumb do not print() anything in functions that process your 
data. Have them return something instead, like a list of titles that may be 
searched, sorted, or filtered later. The final result of this can then be 
printed or displayed in a GUI or webpage, written to a file, or saved in a 
database.

In that spirit I would rewrite your getbooks() function as

def get_book_titles(xmldata):
books = xmldata.xpath("//catalog")[0]
return [book.findtext("title") for book in books]

Now you have something to work with. You provide an xml tree and get a list 
of book titles. As long as you don't change that you can rewrite the 
implementation without breaking the rest of your script.

Now let's suppose we want to find all titles containing some words the user 
can provide. We break the task into tiny subtasks:

- break a string into words
- search a string for words
- filter a list of titles

def get_words(title):
return title.split()

def has_words(words, title):
title_words = get_words(title)
return all(word in title_words for word in words)

def find_matching_titles(titles, words):
return [title for title in titles if has_words(title, words)]


We can check if the above works with a few lines of code:

catalog = etree.parse("example.xml")
titles = get_book_titles(catalog)
print(find_matching_titles(titles, ["Guide"]))

Seems to work. What we really should be doing is to write unit tests for 
every function. If turns out that our program sometimes doesn't work as we 
would like it to we can identify the lacking function and improve only that.
Let's say you want to make the search case-insensitive. That should be 
possible by having get_words() return case-folded strings. 

Finally we can add a simple user interface:

def lookup_title(titles):
while True:
try:
words = get_words(input("Print titles containing all words: "))
except EOFError:
break
matching_titles = find_matching_titles(titles, words)
if matching_titles:
for i, title in enumerate(matching_titles):
print(i, title)
else:
print("no matches")
print()
print("That's all, folks")


if __name__ == "__main__":
catalog = etree.parse("example.xml")
titles = get_book_titles(catalog)
lookup_title(titles)

If instead of just titles you want to process book objects

class Book:
def __init__(self, title, author):
self.title = title
self.author = author

you can only reuse some of the functions, but you can keep the structure of 
the script. For example get_book_titles() could be replaced with

def get_books(xmldata):
books = xmldata.xpath("//catalog")[0]
return [
Book(
book.findtext("title"),
book.findtext("author")
)
for book in books
]

and the filter function could be modified

def find_matching_titles(books, words):
return [book for book in book if has_words(book.title, words)]

You may even change the above to search words in an arbitrary attribute of 
the book instance

def find_matching_books(books, words, attribute):
return [
book for book in books
if has_words(getattr(book, attribute), words)
]


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


Re: [Tutor] Help!

2018-04-19 Thread Neil Cerutti
On 2018-04-19, Steven D'Aprano  wrote:
> Some program ideas...
>
> Encrypt and decrypt text using a Caesar Cipher.
>
> https://en.wikipedia.org/wiki/Caesar_cipher
>
> or one of many other simple ciphers.
>
> Calculate the prime numbers up to a certain limit.
>
> https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
>
> Play "Guess the number" games.
>
> Play 20 Questions.
>
> (Advanced)
> Given the formula for a (simple) molecule like H20, print the names of 
> the elements in the molecule, their atomic weights, and the total 
> molecular weight.
>
> Convert between temperature scales (degrees Celsius, Fahrenheit, and 
> Kelvin).

Excellent suggestions.

It could also be fun to build a text-based solitaire program of
your choice.

-- 
Neil Cerutti

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


Re: [Tutor] beginning encryption

2018-04-19 Thread Emil Natan
if char != alphabet:

should be

if char not in alphabet:

Otherwise you are comparing char with alphabet. What you want to do if to
check if char is in alphabet.

Emil

On Thu, Apr 19, 2018 at 2:39 AM, Roger Lea Scherer  wrote:

> I am absolutely stumped. I've tried a number of different scenarios and
> copied the answer more than I like, but I still can't figure this out. I
> don't want to copy the answer verbatim because then I won't learn.
>
> I'm doing the beginning cipher, mix up the letters routine. I get the
> entire Gettysburg address with no alterations in this form of the code (and
> a few others I've tried). I do not receive any error, but I expect the
> character in the Gettysburg address to change to the index position in the
> encryption variable.
>
> What am I not getting?
>
> Thank you as always.
>
>
>
> address = """Four score and seven years ago our fathers brought forth on
> this continent, a new nation,
> conceived in Liberty, and dedicated to the proposition that all men are
> created equal.
> Now we are engaged in a great civil war, testing whether that nation, or
> any nation so conceived
> and so dedicated, can long endure. We are met on a great battle-field of
> that war. We have come
> to dedicate a portion of that field, as a final resting place for those who
> here gave their lives that
> that nation might live. It is altogether fitting and proper that we should
> do this.
> But, in a larger sense, we can not dedicate -- we can not consecrate -- we
> can not hallow -- this ground.
> The brave men, living and dead, who struggled here, have consecrated it,
> far above our poor power
> to add or detract. The world will little note, nor long remember what we
> say here, but it can never
> forget what they did here. It is for us the living, rather, to be dedicated
> here to the unfinished work
> which they who fought here have thus far so nobly advanced. It is rather
> for us to be here dedicated
> to the great task remaining before us -- that from these honored dead we
> take increased devotion
> to that cause for which they gave the last full measure of devotion -- that
> we here highly resolve
> that these dead shall not have died in vain -- that this nation, under God,
> shall have a new birth
> of freedom -- and that government of the people, by the people, for the
> people, shall not perish
> from the earth."""
>
> alphabet = "abcdefghijklmnopqrstuvwxyz"
> encryption = "nopqrstuvwxyzabcdefghijklm"
>
>
> def encryptor(address):
> encrypted = ""
> for char in address:
> if char != alphabet:
> encrypted += char
> else:
> pos = alphabet.index(char)
> encrypted += encryption[pos]
> print(encrypted)
>
> encryptor(address)
>
>
> --
> Roger Lea Scherer
> 623.255.7719
> ___
> 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] File handling Tab separated files

2018-04-19 Thread Niharika Jakhar
Hi again
I tried re-writing the code with all your advices(i assume to cover all of
them). I have extended the code a little bit to store the data in the form
of lists and am trying to access it.
I also changed the file name to BioGRID.txt

Here's what I wrote(Please ignore the identation, there was no such error,
it's just the e-mail thingy.):

import csv
class BioGRIDReader:
def __init__(self, filename):
with open('filename', 'rb') as f:
self.reader = csv.reader(f, delimiter='\t')
self.object_ = self.reader.split['\n']
for row in range(len(object_)):
for r in range(len(row)):
if (r[-1] == r[-2]):
return (r[2], r[3]) #returns pair of taxon ids






a = BioGRIDReader('BioGRID.txt')
print(a.object[:100])




here's what the compiler says:
Traceback (most recent call last):
  File "./BioGRIDReader.py", line 17, in 
a = BioGRIDReader('BioGRID.txt')
  File "./BioGRIDReader.py", line 4, in __init__
with open('filename', 'rb') as f:
IOError: [Errno 2] No such file or directory: 'filename'





I am extremely sorry if I have repeated a same mistake again, but I did
what I understood.
Thanks again for the links and I am not allowed to use different packages
and have to strictly use the standard python library. But thanks anyway, it
will be of great help in future. :)


On Thu, Apr 19, 2018 at 4:50 PM, Mats Wichmann  wrote:

> On 04/19/2018 07:57 AM, Wolfgang Maier wrote:
> > On 04/19/2018 10:45 AM, Niharika Jakhar wrote:
> >> Hi
> >> I want to store a file from BioGRID database (tab separated file, big
> >> data)
> >> into a data structure(I prefer lists, please let me know if another
> would
> >> be better) and I am trying to print the objects.
> >> Here’s my code:
> >> class BioGRIDReader:
> >>  def __init__(self, filename):
> >>  with open('filename', 'r') as file_:
> >>  read_data = f.read()
> >>  for i in file_ :
> >>  read_data = (i.split('\t'))
> >>  return (objects[:100])
> >>
> >> a = BioGRIDReader
> >> print (a.__init__(test_biogrid.txt))
> >>
> >
> > In addition to your immediate problem, which Steven explained already,
> > you will run into more issues with the posted code:
>
> In addition to this low level advice, let me observe that whenever the
> term "big data" is tossed into the discussion, you want to consider
> whether reading it all in to Python's memory into a "simple" data
> structure in one go is what you want to do.  You may want to look into
> the Pandas project (possibly after spending a little more time becoming
> comfortable with Python itself first):
>
> https://pandas.pydata.org/
>
> Pandas has its own file handling code (particularly, a read_csv
> function) which might end up being useful.
>
>
> Also quite by chance, I happen to know there's an existing project to
> interact with the BioGRID web service, have no idea if that would be a
> match for any of your needs.  A quick google to refind it:
>
> https://github.com/arvkevi/biogridpy
>
> ___
> 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] Fwd: File handling Tab separated files

2018-04-19 Thread Brian Lockwood

>> 
>> Here are some fixes
>> 
>> filename is a variable and hence should not be in quotes.
>> file_ is then called ‘f’ on the next line.
>> The indenting is a bit wrong but this may just be your email.
>> 
>> the line read_data … should be followed by something that appends the 
>> read_data to “object” which should be declared earlier.
>> 
>>> On 19 Apr 2018, at 09:45, Niharika Jakhar >> > wrote:
>>> 
>>> Hi
>>> I want to store a file from BioGRID database (tab separated file, big data)
>>> into a data structure(I prefer lists, please let me know if another would
>>> be better) and I am trying to print the objects.
>>> Here’s my code:
>>> class BioGRIDReader:
>>>   def __init__(self, filename):
>>>   with open('filename', 'r') as file_:
>>>   read_data = f.read()
>>>   for i in file_ :
>>>   read_data = (i.split('\t'))
>>>   return (objects[:100])
>>> 
>>> a = BioGRIDReader
>>> print (a.__init__(test_biogrid.txt))
>>> 
>>> 
>>> 
>>> 
>>> Here's what the terminal says:
>>> Traceback (most recent call last):
>>> File "./BioGRIDReader.py", line 23, in 
>>>   print (a.__init__(test_biogrid.txt))
>>> NameError: name 'test_biogrid' is not defined
>>> 
>>> The file named test_biogrid.txt do exist in the same folder as this program.
>>> 
>>> I am unable to go further with this code. Kindly help me out.
>>> 
>>> 
>>> Thanks and regards
>>> NIHARIKA
>>> ___
>>> 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] File handling Tab separated files

2018-04-19 Thread Alan Gauld via Tutor
On 19/04/18 17:50, Niharika Jakhar wrote:
> Hi again
> I tried re-writing the code with all your advices(i assume to cover all of
> them). I have extended the code a little bit to store the data in the form
> of lists and am trying to access it.
> I also changed the file name to BioGRID.txt
> 
> Here's what I wrote(Please ignore the identation, there was no such error,
> it's just the e-mail thingy.):
> 
> import csv
> class BioGRIDReader:
> def __init__(self, filename):
> with open('filename', 'rb') as f:

Notice 'filename' is in quotes which means its a literal string
so Python looks for a file called 'filename'. It can't find one.
You really want to use the file name stored in the variable
called filename so remove the quotes. You need to review the
difference between variable names and string literals.

> self.reader = csv.reader(f, delimiter='\t')
> self.object_ = self.reader.split['\n']
> for row in range(len(object_)):
> for r in range(len(row)):

This is almost certainly wrong.
The first for sets row to be a number in the range(len...)
The second for line tries to iterate over the len(row)
which will be a very small number - and hence have
no len() value.

> if (r[-1] == r[-2]):

And this is probably wrong too even if r from the
line above was to be a small integer. You can't index
an integer.

Now can you express what you really want to do?

I suspect something like:

for row in self.object:# or self.object.split() maybe?
if row[-1] == row[-2]  # last two chars are the same
   return row[2],row[3]   # 3rd and 4th chars

HTH

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


[Tutor] anomaly

2018-04-19 Thread D . V . N . Sarma డి . వి . ఎన్ . శర్మ
I have a situation in which the same code gives an error in idle but works
in qtconsole
regards,

*​in idle*
v = np.zeros(len(x))

for i in range(len(x)):
if x[i] < 1.0:
v[i] = 0
else:
v[i] = V
print v​

 RESTART: C:\Users\SHARMA\Documents\Python Scripts\sqwell.py


Traceback (most recent call last):
  File "C:\Users\SHARMA\Documents\Python Scripts\sqwell.py", line 45, in

if x[i] < 1.0:
ValueError: The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()

​*in qtconsole​*

v = np.zeros(len(x))

for i in range(len(x)):
if x[i] < 1.0:
v[i] = 0
else:
v[i] = V
print v

[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. 20. 20. 20. 20.
 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20.
 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20. 20.
 20. 20. 20. 20. 20. 20. 20. 20. 20. 20.]

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


Re: [Tutor] anomaly

2018-04-19 Thread Steven D'Aprano
On Fri, Apr 20, 2018 at 10:23:37AM +0530, D.V.N.Sarma డి.వి.ఎన్.శర్మ wrote:
> I have a situation in which the same code gives an error in idle but works
> in qtconsole

Probably different versions of Python running.

What does this say in each of them?


import sys
print (sys.version)
print (sys.executable)



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