[Tutor] If loop conditions

2016-03-01 Thread Dimitar Ivanov
Hello everyone!

First time using this mailing list so please excuse me in advance if this
mail is not structured properly.

I'm going through a Python course by Google, so far I've gotten to the
lists chapter and I've been trying to wrap my head around an exercise all
afternoon long, eventually was forced to look up the answer.

In the following exercise, Google requires you to look up words from a list
and count how many of the words are longer or equal to 2 characters where
the first and last letter match.

I've been trying to assign string slices to variables and then use those
variables inside the following if statement:

def match_ends(words):
+  for word in words:
+length=len(word)
+  newList=[]
+  firstChar=word[0:]
+  lastChar=word[:-1]
+if [ length >= 2 and firstChar == lastChar ]:
+newList.append(word)
+else:
+  break
+  newListCount=len(newList)
+  return newListCount

This was returning quite funky results such as counting each letter within
a word as a different word, not picking up the start/end of string etc.

Eventually I looked up the solution since I tracked down my problem to how
I made my if statement and, of course, it turned out to be much simpler
than usual.

+  if len(word) >= 2 and word[0] == word[-1]:

I tried putting brackets around the conditions and that broke the result
again so I came to wonder what's the difference between statements written
without brackets and those within brackets (such as my original code block)?

I'm sorry if I didn't make it completely clear, please let me know if you
need any further information.

Thank you!

Dimitar



-- 
Thanks,
Dimitar

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


Re: [Tutor] Removing Content from Lines....

2016-03-26 Thread Dimitar Ivanov
Hello everyone,

First time using the mailing list to give a suggestion, apologies in
advance if it's not appropriate :)

Considering you're looking for specific string, I'd recommend maybe looking
into the Regular Expressions. You could use something like:

#At the top of the file
import re

#Within your function
target = open(filename, 'rU')
# This will read the file as a whole string, careful, it loads the file
into memory so if it's a big file, it might cause troubles
text = target.read()
# The regular expression should match every single tag and its content
within the file, using '.*?' will match each character without being greedy.
match = re.findall(r'.*?', text)

Afterwards, you can use something like:

for i in match:
  print i

This should print out all the matches to Stdout so you can verify the RegEx
behaves as expected. To write the matches in a new file, you could do:
  new = open('Matches', 'w') ## This will create a new file called
'Matches' for writing
  for i in match:
new.write(i + '\n') ## Each match of the RegEx will be written on a new
line in the Matches file.

At the end, my function looks like this:

def textfind(filename):
  target = open(filename, 'rU')
  text = target.read()
  match = re.findall(r'.*?', text)
  new = open('Matches', 'w')
  for i in match:
new.write(i + '\n')

Regards,
Dimitar

-- 
Thanks,
Dimitar

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


[Tutor] Declaring variables

2016-04-07 Thread Dimitar Ivanov
Hello everyone,

I have a (hopefully) quick and easy to explain question. I'm currently
using MySQLdb module to retrieve some information from a database. In my
case, the result that's being yield back is a single line.

As far as my understanding goes, MySQLdb function called 'fetchone()'
returns the result as a tuple. Problem is, the tuple has some unnecessary
characters, such as an additional comma being returned. In my case:

>>>  idquery = 'select id from table;'
>>>  cur = mysql.cursor()
>>>  cur.execute(idquery)
>>>  id = cur.fetchone()
>>>  print id
('idinhere',)

I stumbled across an example given like this:

>>>  (id,) = cur.fetchone()

So I decided to give it a try and the result is exactly what I need:

>>>  (id,) = cur.fetchone()
>>>  print id
idinhere

My question is - can I reliably use this method? Is it always going to
return the string between the brackets as long as I define the variable
with '(,)'? I'm planning to use another query that will be using the result
from this one and then update another database with this result but I must
be sure that the variable will always be the string in and between the
brackets otherwise I'm risking to mess up a lot of things big time.

A backup plan I had was to use the following:

>>>  id = cur.fetchone()
>>>  for x in id:
>>>id = x

But if the method above is certain to always return only the value I need,
I find it to be a far more elegant solution.

Also, just to clarify things for myself - what does this method of
declaring variables do exactly? I'm sorry if this isn't the right place the
ask and if this has been documented clearly already, I'm not sure what to
use as a search term in order to find an answer.

Thanks a lot in advance! I hope I posted all the details needed and my
question is easy to comprehend.

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


[Tutor] [Python 3] Threads status, join() and Semaphore queue

2018-11-19 Thread Dimitar Ivanov
Hello everyone,

I'm having a hard time getting my head around threads so I was hoping
someone who has better understanding of their underlying functionality
could lend me a helping hand, in particular how threads work with each
other when using thread.join() and Semaphore set with maximum value. I'll
try to keep it as clear and concise as possible, but please don't hesitate
to ask if anything about my approach is unclear or, frankly, awful.

I'm writing a script that performs a couple of I/O operations and CLI
commands for each element in a list of IDs. The whole process takes a while
and may vary based on the ID, hence the threading approach sounded like the
best fit since next ID can start once space has freed up. I'm parsing an
extract of my code below and will explain what I can't properly understand
underneath.

Note: Please ignore any syntax typos, I'm rewriting the code to make it
suitable for here.


file1.py
-
ids = []
threadsPool = []
for id in ids:
  thread = threading.Thread(target=file2.runStuff, name=str(id), args=(id,
))
  threadsPool.append(thread)
for thread in threadsPool:
  thread.start()
for thread in threadsPool:
  print(thread.enumerate())
  print("Queuing thread" + str(thread))
  thread.join()

file2.py
--
queue = threading.Semaphore(2)
def runStuff(id):
  queue.acquire()
  print("Lock acquired for " + str(id))
  file3.doMoreStuff()
  file4.evenMoreStuff()
  queue.release()


Onto my confusion - as long as I don't try to print information about the
thread that's being queued or the total amount of threads using
.enumerate(), the script is working absolutely flawlessly, each thread that
doesn't have a lock is waiting until it acquires it and then moves on. I
decided it'd be nice to be able to provide more information about which
thread starts next and how many threads are active right now (each can take
a different amount of time), however, when I tried to do that, my log was
showing me some pretty funky output which at first made me believe I've
messed up all my threads, example:


<<  2018-11-19 15:01:38,094 file2 [ID09] INFO - Lock acquired for
ID09 < this is from file2.py
-- some time later and other logs in here -
[<_MainThread(MainThread, started 140431033562880)>, ] < output from thread.enumerate(), file1.py
<<  2018-11-19 15:01:38,103 file1 [MainThread] DEBUG - Queuing thread -
 < output from print() right
after thread.enumerate()


After some head scratching, I believe I've finally tracked down the reason
for my confusion:

The .start() loop starts the threads and the first 2 acquire a lock
immediately and start running, later on the .join() queue puts the rest in
waiting for lock, that's fine, what I didn't realize, of course, is that
the .join() loop goes through threads that have already been instantly
kicked off by the .start() loop (the first 2 threads since Semaphore allows
2 locks) and then my print in that loop is telling me that those threads
are being queued, except they aren't since they are already running, it's
just my text is telling me that, since I wasn't smart enough to realize
what's about to happen, as seen below:

<<  2018-11-19 15:01:33,094 file1.py [MainThread] DEBUG - Queuing thread -
 <--- makes it clear the thread has
already even finished

Which finally gets me to my cry for help - I know I can't modify the
threadsPool list to remove the threads already created on the fly, so I can
have only the ones pending to be queued in the 2nd loop, but for the life
of me I can't think of a proper way to try and extract some information
about what threads are still going (or rather, have finished since
thread.enumerate() shows both running and queued).

I have the feeling I'm using a very wrong approach in trying to extract
that information in the .join() loop, since it only goes back to it once a
thread has finished, but at the same time it feels like the perfect timing.
I feel like (and I might be very wrong) if I could only have the threads
that are actually being queued in there (getting rid of the ones started
initially), my print(thread) will be the absolute sufficient amount of
information I want to display.

And just in case you are wondering why I have my threads starting in
file1.py and my Semaphore queue in file2.py, it's because I wanted to split
the runStuff(id) function in a separate module due to its length. I don't
know if it's a good way to do it, but thankfully the Python interpreter is
smart enough to see through my ignorance.

I'm also really sorry for the wall of text, I just hope the information
provided is enough to clear up the situation I'm in and what I'm struggling
with.

Thank you in advance and with kindest regards,
Dimitar
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] [Python 3] Threads status, join() and Semaphore queue

2018-11-24 Thread Dimitar Ivanov
Hi Cameron,

Massive apologies for the delayed answer!

Your explanation definitely clears up quite a bit of my misunderstanding,
thank you for that!

There was a reason why I shy away from using Queue, but for the life of me
I can't remember right now what that reason was. I will have to modify my
code using your example and give it another try, I will make sure to let
you know if I run into any issues or additional questions. :)

Regards,
Dimitar

On Tue, 20 Nov 2018 at 08:39, Cameron Simpson  wrote:

> On 19Nov2018 23:52, Dimitar Ivanov  wrote:
> >I'm having a hard time getting my head around threads so I was hoping
> >someone who has better understanding of their underlying functionality
> >could lend me a helping hand, in particular how threads work with each
> >other when using thread.join() and Semaphore set with maximum value. I'll
> >try to keep it as clear and concise as possible, but please don't hesitate
> >to ask if anything about my approach is unclear or, frankly, awful.
> >
> >I'm writing a script that performs a couple of I/O operations and CLI
> >commands for each element in a list of IDs. The whole process takes a
> while
> >and may vary based on the ID, hence the threading approach sounded like
> the
> >best fit since next ID can start once space has freed up. I'm parsing an
> >extract of my code below and will explain what I can't properly understand
> >underneath.
>
> I'm just going to scatter a few random remarks about your code in here
> with your listing before addressing your other queries lower down...
>
> >file1.py
> >-
> >ids = []
> >threadsPool = []
> >for id in ids:
>
> The name "id" is unfortunate as it conflicts with the id() builtin
> function. Maybe "element_id"? Wordier, but also more clear.
>
> >  thread = threading.Thread(target=file2.runStuff, name=str(id), args=(id,
> >))
> >  threadsPool.append(thread)
> >for thread in threadsPool:
> >  thread.start()
>
> You could start each thread right after creating it if you wished.
>
> >for thread in threadsPool:
> >  print(thread.enumerate())
>
> "enumerate" is a function from the threading module, not a method of a
> Thread. So try:
>
>   print(threading.enumerate())
>
> Frankly I'm surprised that "thread.enumerate" works at all.
>
> >  print("Queuing thread" + str(thread))
> >  thread.join()
> >
> >file2.py
> >--
> >queue = threading.Semaphore(2)
>
> I'd be disinclined to call this a "queue", which usually implies a FIFO
> list of some variety: put things onto it, and pull things off it,
> usually first in first off. Maybe just "sem" or "thread_capacity" or
> something?
>
> >def runStuff(id):
> >  queue.acquire()
> >  print("Lock acquired for " + str(id))
> >  file3.doMoreStuff()
> >  file4.evenMoreStuff()
> >  queue.release()
> >
> >Onto my confusion - as long as I don't try to print information about the
> >thread that's being queued or the total amount of threads using
> >.enumerate(), the script is working absolutely flawlessly, each thread
> that
> >doesn't have a lock is waiting until it acquires it and then moves on. I
> >decided it'd be nice to be able to provide more information about which
> >thread starts next and how many threads are active right now (each can
> take
> >a different amount of time), however, when I tried to do that, my log was
> >showing me some pretty funky output which at first made me believe I've
> >messed up all my threads, example:
> >
> >
> ><<  2018-11-19 15:01:38,094 file2 [ID09] INFO - Lock acquired for
> >ID09 < this is from file2.py
> >-- some time later and other logs in here -
> >[<_MainThread(MainThread, started 140431033562880)>,  >140430614177536)>] < output from thread.enumerate(), file1.py
> ><<  2018-11-19 15:01:38,103 file1 [MainThread] DEBUG - Queuing thread -
> > < output from print() right
> >after thread.enumerate()
> >
> >After some head scratching, I believe I've finally tracked down the reason
> >for my confusion:
> >
> >The .start() loop starts the threads and the first 2 acquire a lock
> >immediately and start running, later on the .join() queue puts the rest in
> >waiting for lock, that's fine, what I didn't realize, of course, is that
> >the .join() loop goes through threads that have already been instantly
> >kicked off by th