Visit Our UNIX and Linux User Community


Python Combing Two Commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python Combing Two Commands
# 1  
Old 06-24-2015
Python Combing Two Commands

I have been digging deeper into Python and want to make my code as efficient as possible. The less line of code the better so I have been experimenting and wanted to ask the Python gurus if this is possible. So:

Code:
...
...
In [109]: kbfileurl = re.search('<p>For more information about this update.*</p>', tbull.text.encode('utf8'))
In [110]: kbfileurl.group()
Out[110]: <p>For more information about this update, see <a href="https://support.microsoft.com/kb/3020393">Microsoft Knowledge Base Article 3020393</a>.</p>'

So based on the string of the url that I parsed out of the html page, I would like to pull only in a one-liner:
https://support.microsoft.com/kb/3020393
So is it possible to combine kbfileinfo,group with re,compile:
Code:
kbfileurl.group().encode('ascii')re.compile(r'\bhttps://support.microsoft.com/kb/d+\b')

to parse out:
https://support.microsoft.com/kb/3020393
??

Last edited by metallica1973; 06-24-2015 at 03:13 PM..
# 2  
Old 06-24-2015
Quote:
The less line of code the better
Quite the opposite.

Some of the concepts in python's philosophy are:
Quote:
Beautiful is better than ugly.
Simple is better than complex.
Readability counts.
A one-liner quite possible goes against any of them.

Furthermore, lines should be limited to a maximum of 79 characters long. Preferable 72.

It is true that when readability is not hindered it is possible to daisy chain methods output of an object to another method using a period. This is not the case.

Last edited by Aia; 06-24-2015 at 04:06 PM.. Reason: grammar
# 3  
Old 06-24-2015
Thanks for the reply. Awesome advice but after playing around with it, I did a small modification and came up with but not exactly a one-liner:
Code:
In [201]: kbfileurl = re.search('<p>For more information about this update.*</p>', tbull.text.encode('utf8')).group()

In [202]: kbfileurl
Out[202]: '<p>For more information about this update, see <a href="https://support.microsoft.com/kb/3020393">Microsoft Knowledge Base Article 3020393</a>.</p>'

In [203]: kburl = re.search(r'\bhttps://support.microsoft.com/kb/\d+\b', kbfileurl).group(0)

In [204]: kburl
Out[204]: 'https://support.microsoft.com/kb/3020393'

# 4  
Old 06-24-2015
If you keep at it, eventually, you'll get here:

Code:
print(re.search(r'\bhttps://support.microsoft.com/kb/\d+\b', re.search('<p>For more information about this update.*</p>', tbull.text.encode('utf8')).group()).group(0))

Is it possible? You can put a whole file code in one line. Can you know right away if I have included all the necessary closing parenthesis, or if I passed all the needed arguments? I do not think so.

Code:
>>> import this

 The Zen of Python

    Beautiful is better than ugly.
    Explicit is better than implicit.
    Simple is better than complex.
    Complex is better than complicated.
    Flat is better than nested.
    Sparse is better than dense.
    Readability counts.
    Special cases aren't special enough to break the rules.
    Although practicality beats purity.
    Errors should never pass silently.
    Unless explicitly silenced.
    In the face of ambiguity, refuse the temptation to guess.
    There should be one-- and preferably only one --obvious way to do it.
    Although that way may not be obvious at first unless you're Dutch.
    Now is better than never.
    Although never is often better than *right* now.
    If the implementation is hard to explain, it's a bad idea.
    If the implementation is easy to explain, it may be a good idea.
    Namespaces are one honking great idea -- let's do more of those!

---------- Post updated at 02:06 PM ---------- Previous update was at 01:47 PM ----------

By the way, the Zen of Python, is not just a nice advice, it is the expression of the language's philosophy, and if you pay attention, it is doing what it preaches.
Example:

Line 4
Code:
Explicit is better than implicit.

Line 19
Code:
If the implementation is hard to explain, it's a bad idea.

Line 20 applies the principle of explicit is better than implicit. Instead of letting the reader assume that the opposite of the previous statement is good, it just says it.
Code:
If the implementation is easy to explain, it may be a good idea.


Last edited by Aia; 06-24-2015 at 07:34 PM.. Reason: Grammar
# 5  
Old 06-24-2015
Can't see anything about redundancy in the Zen list!

Line 20 seems to be useless fluff to me and could just a well be written:

Code:
If the implementation is easy to explain, today may or may not be Tuesday.

# 6  
Old 06-29-2015
I love it. Thanks for all the replies.
# 7  
Old 06-30-2015
One liner Python code is not easy:-
OSX 10.7.5, running python 3.4 inside a default bash terminal.
This is why I have switched to shell scripting...
Code:
Last login: Tue Jun 30 15:55:49 on ttys000
AMIGA:barrywalker~> python3.4
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x=0
>>> while True:
...         print(x)
... 
0
0
0
0
.
.
0
0
^C0
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
KeyboardInterrupt
>>> x=0; while True: print(x)
  File "<stdin>", line 1
    x=0; while True: print(x)
             ^
SyntaxError: invalid syntax
>>> _

Notice the second attempt above as a 1 liner...
Yeah I know the assignment is not a statement, but neither are they in bash, but this is easy in the shell...
Miss out the assignment and put it on a separate line and it will run...
Code:
Last login: Tue Jun 30 15:55:49 on ttys000
AMIGA:barrywalker~> python3.4
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 00:54:21) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x=0
>>> while True: print(x)
... 
0
0
0
0
.
.
0
0
^C0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyboardInterrupt
>>> _


Previous Thread | Next Thread
Test Your Knowledge in Computers #398
Difficulty: Medium
HP-UX offers operating system-level virtualization features such as hardware partitions, isolated OS virtual partitions on cell-based servers, and HP Integrity Virtual Machines (HPVM) on all Integrity servers.
True or False?

9 More Discussions You Might Find Interesting

1. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies

2. Windows & DOS: Issues & Discussions

How to execute python script on remote with python way..?

Hi all, I am trying to run below python code for connecting remote windows machine from unix to run an python file exist on that remote windows machine.. Below is the code I am trying: #!/usr/bin/env python import wmi c = wmi.WMI("xxxxx", user="xxxx", password="xxxxxxx")... (1 Reply)
Discussion started by: onenessboy
1 Replies

3. Shell Programming and Scripting

**python** unable to read the background color in python

I am working on requirement on spreadsheet in python scripting. I have a spreadsheet containing cell values and with background color. I am able to read the value value but unable to get the background color of that particular cell. Actually my requirement is to read the cell value along... (1 Reply)
Discussion started by: giridhar276
1 Replies

4. Shell Programming and Scripting

How to combing output of cut commands with a delimiter?

While looping through a file, I am cutting different length of characters (based on their length) like columns and want to produce the output in a separate file with different columns being separated by a comma. How to achieve this with an online command. I don't want to create multiple variables... (8 Replies)
Discussion started by: mady135
8 Replies

5. Shell Programming and Scripting

[Solved] How to create a script by combing two files?

I am trying to generate a csv file for utilization of each project directory. there are 10 filesystem and for each filesystem there are 16 directory. i was trying to create a script so i created two file. one is filesystem and one is project. so file looks like cat filesystems /app1 /app2... (10 Replies)
Discussion started by: anshu ranjan
10 Replies

6. Shell Programming and Scripting

combing two lines

Hi list, I have to combine two lines and adding the " - " in between. So: Here's sentence A Here's Senctence B Here's sentence A - Here's Senctence B I know how to us SED: sed '$!N;s/\n/ /' for combining tow lines. But how do I insert the " - " correctly? Thanks! Martijn (os... (4 Replies)
Discussion started by: M474746
4 Replies

7. Shell Programming and Scripting

a tip in python commands >> and <<

q = ((i + 2) * 6) >> 3 what's the meaning in python? (3 Replies)
Discussion started by: kazikamuntu
3 Replies

8. Red Hat

Writing simple python setup commands

Building software in most languages is a pain. Remember ant build.xml, maven2 pom files, and multi-level makefiles? Python has a simple solution for building modules, applications, and extensions called distutils. Disutils comes as part of the Python distribution so there are no other packages... (0 Replies)
Discussion started by: Linux Bot
0 Replies

9. Shell Programming and Scripting

Exec. commands in python

How would i do if i'd want to execute a command in a python script or programme ? (1 Reply)
Discussion started by: J.P
1 Replies

Featured Tech Videos