Re: [Tutor] Script to search in string of values from file A in file B

2012-05-09 Thread BRAGA, Bruno
On Thursday, May 10, 2012, Afonso Duarte  wrote:
> Dear All,
>
>
>
> I’m new to Python and started to use it to search text strings in big
(>500Mb) txt files.
>
> I have a list on text file (e.g. A.txt) that I want to use as a key to
search another file (e.g. B.txt), organized in the following way:
>
>
>
> A.txt:
>
>
>
> Aaa
>
> Bbb
>
> Ccc
>
> Ddd
>
> .
>
> .
>
> .
>
>
>
> B.txt
>
>
>
> Bbb
>
> 1234
>
> Xxx
>
> 234
>
>
>
>
>
> I want to use A.txt to search in B.txt and have as output the original
search entry (e.g. Bbb) followed by the line that follows it in the B.txt
(e.g.  Bbb / 1234).
>
> I wrote the following script:
>
>
>
>
>
> object = open(B.txt', 'r')
>
> lista = open(A.txt', 'r')
>
> searches = lista.readlines()
>
> for line in object.readlines():
>
>  for word in searches:
>
>   if word in line:
>
>print line+'\n'
>
>
>
>
>
>
>
> But from here I only get the searching entry and not the line afterwards,
I tried to google it but I got lost and didn’t manage to do it.
>
> Any ideas ? I guess that this is basic scripting but I just started .

Not sure I understood the question... But:
- are you trying to "grep" the text file? (simpler than programming in
python, IMO)
- if you have multiple matches of any of the keys from A file in a sungle
line of B file, the script above will print it multiple times
- you need not add new line (\n) in the print statement, unless you want it
to print a blank line between results

Based on the example you gave, the matching Bbb value in B and A are the
same, so actually line is being printed, but it is just the same as word...


>
>
>
> Best
>
>
>
> Afonso

-- 
Sent from Gmail Mobile
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Console Application - Key Events

2012-05-08 Thread BRAGA, Bruno
Yeah, what you say makes sense... I was hoping that there was a nice way of
doing it, but I will just have to stick with "running the command yourself".

I thought there could be a solution, maybe based on Ncurses, or even on the
"read" command, but they can not get the keys pressed if the application is
on background... and to do it on the foreground with the terminal on is
crazy...

Thanks anyway, it was very helpful!

--
*Braga, Bruno*
www.brunobraga.net
bruno.br...@gmail.com


On Tue, May 8, 2012 at 5:59 PM, Alan Gauld wrote:

> On 08/05/12 02:44, BRAGA, Bruno wrote:
>
>> I would like to know if there is any "easy" way to handle events (such
>> as mouse movements, keyboard keys pressed, etc) in console based python
>> applications?
>>
>
> You need to be more specific about what you mean.
> Consoles don't have mice. Keyboard events are easily
> handled (see my tutorial on event driven apps for
> more info and examples)
>
> You may be confusing the concept of a console running
> inside a window of a GUI system. In that case the
> window receives events and the window manager can
> handle them. But the python application running
> inside that terminal emulator is completely unaware
> of them.
>
> There are also event driven frameworks for consoles
> that can capture events (Borland had one such for DOS),
> but it is the framework not Python that is aware of
> these things.
>
>
>  More specifically, I am working on a screensaver for terminals
>> (http://termsaver.info), so I would like to simulate the same behaviour
>> of a standard screensaver for the X windows, by:
>>
>
> What is this screensaver going to do? Display an alternative
> screen full of text? Go blank?
>
>   * running on background
>>  * starting some functionality (display a text, etc) if there is no
>>
>>event (mouse or keyboard) for more than N minutes
>>  * stopping the above if there is any movement detected
>>
>
> It is possible to do some of that in a multi process environment
> by running an application in the background, but you would need some kind
> of cooperative behaviour from the foreground app I suspect.
> Which limits applicability.
>
> But the biggest problem you face is just that consoles generally
> do not have any kind of event awareness. Mice etc just don't exist.
> They are not event based. And where the terminal emulator is
> event aware (eg xterm on Linux) the events are not passed on
> to the app except in very limited circumstances - eg pasting
> from a mouse selection - and that is usually just a case of
> injecting the selected text into stdin. And that is deliberate since the
> apps need to be able to work in real dumb terminals (VT100 etc)
> or even teletypes.
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using subprocess to export files in bash

2012-05-08 Thread BRAGA, Bruno
No idea why you would want to do that (looks more complicated in python
than in bash, right?)... but:

f = open("log.txt", "w")
f.write(output)
f.close()

--
*Braga, Bruno*
www.brunobraga.net
bruno.br...@gmail.com


On Wed, May 9, 2012 at 12:18 AM, Rogelio  wrote:

> While reading the subprocess documentation, I found a great example on
> how to call commands with a PIPE
>
> http://docs.python.org/library/subprocess.html
>
> **
> output=`dmesg | grep hda`
> # becomes
> p1 = Popen(["dmesg"], stdout=PIPE)
> p2 = Popen(["grep", "hda"], stdin=p1.stdout, stdout=PIPE)
> p1.stdout.close()  # Allow p1 to receive a SIGPIPE if p2 exits.
> output = p2.communicate()[0]
>
> 
>
> How do I do this and output to a file?
>
> e.g.
>
> output = "dmesg | grep hda > log.txt'
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Curious dictionary printing

2012-05-07 Thread BRAGA, Bruno
Put it simple, dictionaries do not sort. You can use the dict.keys() to get
a list of the dictionary keys, then sort them... there are lots of talks on
this, just google a bit, and you will find fancy ways to do key or value
sorting.

--
*Braga, Bruno*
www.brunobraga.net
bruno.br...@gmail.com


On Tue, May 8, 2012 at 12:31 PM, bob gailer  wrote:

> On 5/7/2012 1:16 PM, Cranky Frankie wrote:
>
>> In 3.2.2 in IDLE I have this dictionary entry:
>>
>> Namath = {"first_name": "Joe", "last_name": "Namath", "phone": "
>> 212-222-",\
>>   "email": "joe.nam...@gmail.com", "stadium": "Shea Stadium"}
>>
>> when I print it:
>>
>> print(Namath)
>>
>> I get:
>>
>> {'phone': '212-222-', 'first_name': 'Joe', 'last_name': 'Namath',
>> 'email': 'joe.nam...@gmail.com', 'stadium': 'Shea Stadium'}
>>
>> Why is it out of order?
>>
>
> May I recommend a different approach to such questions.
>
> When you get an unexpected (undesired)  result try saying - "Oh I see -
> that's how Python does it!"
>
> I want something different. How can I get it?
>
> Then try reading the documentation.
>
> Asking why does it not do what I want is not IMHO the best way to win
> friends here.
>
> Taking this steps further
>  - what does it mean to be "in order". To some it is the order in which
> items are added rather than some collating sequence.
>  - Do you want order by key or by value?
>  - how would you order {1 : 'cat', "a": 3, (2,3): True}?
>
> --
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Console Application - Key Events

2012-05-07 Thread BRAGA, Bruno
Hi tutors,

I would like to know if there is any "easy" way to handle events (such as
mouse movements, keyboard keys pressed, etc) in console based python
applications?

More specifically, I am working on a screensaver for terminals (
http://termsaver.info), so I would like to simulate the same behaviour of a
standard screensaver for the X windows, by:

   - running on background
   - starting some functionality (display a text, etc) if there is no event
   (mouse or keyboard) for more than N minutes
   - stopping the above if there is any movement detected

Any thoughts on this would be highly appreciated.

Thanks!

--
*Braga, Bruno*
www.brunobraga.net
bruno.br...@gmail.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor