7
\$\begingroup\$

How can I make my code more pythonic ? I definitely think there is a way to make this code a lot more readable and clear + shorter...

But I haven't found an effective way. Any techniques I can use to make it more concise and clear?

#!/usr/bin/env python
import requests
import socket
import random
import re
import time


link = "https://100.1.X.X/testpage.html"
ipAddresses = ['100.1.177.1', '100.1.177.2', '100.1.177.3']
superprice = 31867
medprice = 22992
gutdop = 12000

targetsuper = ["QHTxDZ7I56KU", "bXH5ApeO4", "tLu8JAllx8zJfA", "LwJ2UUGcVs_ns"]
targetmedium = ["QHTxDZ7I56KU", "bXH5ApeO4", "tLu8JAllx8zJfA", "LwJ2UUGcVs_ns"]
targetknife = ["QHTxDZ7I56KU", "bXH5ApeO4", "tLu8JAllx8zJfA", "LwJ2UUGcVs_ns"]
targetdoppler = ["QHTxDZ7I56KU", "bXH5ApeO4", "tLu8JAllx8zJfA", "LwJ2UUGcVs_ns"]
targetm = ["QHTxDZ7I56KU", "bXH5ApeO4", "tLu8JAllx8zJfA", "LwJ2UUGcVs_ns"]   
targetmg = ["QHTxDZ7I56KU", "bXH5ApeO4", "tLu8JAllx8zJfA", "LwJ2UUGcVs_ns"]

def SaleProcess(cart, cookie_value):
    print "=========FOUND SOMETHING========="
    print cart
    print cookie_value
def main():
    x = 0
    while True:
        try:
            proc_c = cookie_value
            start = Scanner(cookie_value)
        except:
            print "= Sleeping for 1sec and trying again"
            time.sleep(1)
            continue    

def Scanner(cookie_value):
    x = 0
    while x < 14400:
        for ipAddress in ipAddresses:
            #leave clear for now
            x = x + 1
            try:
                #DO request with ipAddress

                response = request.get(link)

                strResponce = str(response)
                regexp = "&item=(.*?)'>(.*?)<"
                analystid = re.findall(regexp, strResponce)
                regexp = 'addToCart\((.*?)\)'
                price = re.findall(regexp, strResponce)    
                regexp = 'image\/(.*?)\/256fx256f'
                image = re.findall(regexp, strResponce)

                price1,analystid1,image1 = price[:1],analystid[:1],image[:1]
                price2,analystid2,image2 = price[1:2],analystid[1:2],image[1:2]
                price3,analystid3,image3 = price[2:3],analystid[2:3],image[2:3]
                price4,analystid4,image4 = price[3:4],analystid[3:4],image[3:4]
                price5,analystid5,image5 = price[4:5],analystid[4:5],image[4:5]
                price6,analystid6,image6 = price[5:6],analystid[5:6],image[5:6]
                price7,analystid7,image7 = price[6:7],analystid[6:7],image[6:7]
                price8,analystid8,image8 = price[7:8],analystid[7:8],image[7:8]
                price9,analystid9,image9 = price[8:9],analystid[8:9],image[8:9]
                price10,analystid10,image10 = price[9:10],analystid[9:10],image[9:10]

            except:
                print "Request issue"
                x = x + 14400
                break 
            try:
                cart1,analyst1 = str(analystid1).split(",")
                cart2,analyst2 = str(analystid2).split(",")
                cart3,analyst3 = str(analystid3).split(",")
                cart4,analyst4 = str(analystid4).split(",")
                cart5,analyst5 = str(analystid5).split(",")
                cart6,analyst6 = str(analystid6).split(",")
                cart7,analyst7 = str(analystid7).split(",")
                cart8,analyst8 = str(analystid8).split(",")
                cart9,analyst9 = str(analystid9).split(",")
                cart10,analyst10 = str(analystid10).split(",")

                analyst1 = analyst1[2:-3]
                analyst2 = analyst2[2:-3]
                analyst3 = analyst3[2:-3]
                analyst4 = analyst4[2:-3]
                analyst5 = analyst5[2:-3]
                analyst6 = analyst6[2:-3]
                analyst7 = analyst7[2:-3]
                analyst8 = analyst8[2:-3]
                analyst9 = analyst9[2:-3]
                analyst10 = analyst10[2:-3]

                cart1 = cart1[3:-1]
                cart2 = cart2[3:-1]
                cart3 = cart3[3:-1]
                cart4 = cart4[3:-1]
                cart5 = cart5[3:-1]
                cart6 = cart6[3:-1]
                cart7 = cart7[3:-1]
                cart8 = cart8[3:-1]
                cart9 = cart9[3:-1]
                cart10 = cart10[3:-1]

                price1 = str(price1)
                price2 = str(price2)
                price3 = str(price3)
                price4 = str(price4)
                price5 = str(price5)
                price6 = str(price6)
                price7 = str(price7)
                price8 = str(price8)
                price9 = str(price9)
                price10 = str(price10)

                price1 = price1.strip("[]").replace('\'', '')
                price2 = price2.strip("[]").replace('\'', '')
                price3 = price3.strip("[]").replace('\'', '')
                price4 = price4.strip("[]").replace('\'', '')
                price5 = price5.strip("[]").replace('\'', '')
                price6 = price6.strip("[]").replace('\'', '')
                price7 = price7.strip("[]").replace('\'', '')
                price8 = price8.strip("[]").replace('\'', '')
                price9 = price9.strip("[]").replace('\'', '')
                price10 = price10.strip("[]").replace('\'', '')


                (cartid1, tprice1) = price1.split(",")
                (cartid2, tprice2) = price2.split(",")
                (cartid3, tprice3) = price3.split(",")
                (cartid4, tprice4) = price4.split(",")
                (cartid5, tprice5) = price5.split(",")
                (cartid6, tprice6) = price6.split(",")
                (cartid7, tprice7) = price7.split(",")
                (cartid8, tprice8) = price8.split(",")
                (cartid9, tprice9) = price9.split(",")
                (cartid10, tprice10) = price10.split(",")

                cartid1 = str(cartid1)
                cartid2 = str(cartid2)
                cartid3 = str(cartid3)
                cartid4 = str(cartid4)
                cartid5 = str(cartid5)
                cartid6 = str(cartid6)
                cartid7 = str(cartid7)
                cartid8 = str(cartid8)
                cartid9 = str(cartid9)
                cartid10 = str(cartid10)


                totalprice1 = float(tprice1)
                totalprice2 = float(tprice2)
                totalprice3 = float(tprice3)
                totalprice4 = float(tprice4)
                totalprice5 = float(tprice5)
                totalprice6 = float(tprice6)
                totalprice7 = float(tprice7)
                totalprice8 = float(tprice8)
                totalprice9 = float(tprice9)
                totalprice10 = float(tprice10)

                image1 = str(image1)[2:-3]
                image2 = str(image2)[2:-3]
                image3 = str(image3)[2:-3]
                image4 = str(image4)[2:-3]
                image5 = str(image5)[2:-3]
                image6 = str(image6)[2:-3]
                image7 = str(image7)[2:-3]
                image8 = str(image8)[2:-3]
                image9 = str(image9)[2:-3]
                image10 = str(image10)[2:-3]

            except:
                print "Resetting counter"
                x = x + 14400
                break 
            print "==============================="    
            print analyst1,totalprice1
            print analyst2,totalprice2
            print analyst3,totalprice3
            print analyst4,totalprice4
            print analyst5,totalprice5
            print analyst6,totalprice6
            print analyst7,totalprice7
            print analyst8,totalprice8
            print analyst9,totalprice9
            print analyst10,totalprice10

            #if totalprice1 <= 10:
            #        if cart1 == cartid1:
            #            cart = cart1
            #            JustDOit = SaleProcess(cart, cookie_value)

            if any(text in analyst1 for text in targetknife):
                if totalprice1 <= 3000:
                    if cart1 == cartid1:
                        cart = cart1
                        JustDOit = SaleProcess(cart, cookie_value)

            if any(word in analyst1 for word in targetdoppler):
                if image1 in targetmg:
                    if totalprice1 <= superprice:               
                        if cart1 == cartid1:
                            cart = cart1
                            JustDOit = SaleProcess(cart, cookie_value)
                if image1 in targetm:
                    if totalprice1 <= gutdop:               
                        if cart1 == cartid1:
                            cart = cart1
                            JustDOit = SaleProcess(cart, cookie_value)

            if any(string in analyst1 for string in targetsuper):
                if totalprice1 <= superprice:
                    if cart1 == cartid1:
                        cart = cart1
                        JustDOit = SaleProcess(cart, cookie_value)

            if any(string in analyst1 for string in targetmedium):
                if totalprice1 <= medprice:
                    if cart1 == cartid1:
                        cart = cart1
                        JuststDOit = SaleProcess(cart, cookie_value)
            #number2            
            if any(text in analyst2 for text in targetknife):
                if totalprice2 <= 3000:
                    if cart2 == cartid2:
                        cart = cart2
                        JustDOit = SaleProcess(cart, cookie_value)

            if any(word in analyst2 for word in targetdoppler):
                if image2 in targetmg:
                    if totalprice2 <= superprice:               
                        if cart2 == cartid2:
                            cart = cart2
                            JustDOit = SaleProcess(cart, cookie_value)
                if image1 in targetm:
                    if totalprice2 <= gutdop:               
                        if cart2 == cartid2:
                            cart = cart2
                            JustDOit = SaleProcess(cart, cookie_value)

            if any(string in analyst2 for string in targetsuper):
                if totalprice2 <= superprice:
                    if cart2 == cartid2:
                        cart = cart2
                        JustDOit = SaleProcess(cart, cookie_value)  

            if any(string in analyst2 for string in targetmedium):
                if totalprice2 <= medprice:
                    if cart2 == cartid2:
                        cart = cart2
                        JustDOit = SaleProcess(cart, cookie_value)                       
            #number3
            if any(text in analyst3 for text in targetknife):
                if totalprice3 <= 3000:
                    if cart3 == cartid3:
                        cart = cart3
                        JustDOit = SaleProcess(cart, cookie_value)

            if any(word in analyst3 for word in targetdoppler):
                if image3 in targetmg:
                    if totalprice3 <= superprice:               
                        if cart3 == cartid3:
                            cart = cart3
                            JustDOit = SaleProcess(cart, cookie_value)
                if image3 in targetm:
                    if totalprice3 <= gutdop:               
                        if cart3 == cartid3:
                            cart = cart3
                            JustDOit = SaleProcess(cart, cookie_value)

            if any(string in analyst3 for string in targetsuper):
                if totalprice3 <= superprice:
                    if cart3 == cartid3:
                        cart = cart3
                        JustDOit = SaleProcess(cart, cookie_value)

            if any(string in analyst3 for string in targetmedium):
                if totalprice3 <= medprice:
                    if cart3 == cartid3:
                        cart = cart3
                        JustDOit = SaleProcess(cart, cookie_value)                      
            #number4
            if any(text in analyst4 for text in targetknife):
                if totalprice4 <= 3000:
                    if cart4 == cartid4:
                        cart = cart4
                        JustDOit = SaleProcess(cart, cookie_value)

            if any(word in analyst4 for word in targetdoppler):
                if image4 in targetmg:
                    if totalprice4 <= superprice:               
                        if cart4 == cartid4:
                            cart = cart4
                            JustDOit = SaleProcess(cart, cookie_value)
                if image1 in targetm:
                    if totalprice4 <= gutdop:               
                        if cart4 == cartid4:
                            cart = cart4
                            JustDOit = SaleProcess(cart, cookie_value)

            if any(string in analyst4 for string in targetsuper):
                if totalprice4 <= superprice:
                    if cart4 == cartid4:
                        cart = cart4
                        JustDOit = SaleProcess(cart, cookie_value)

            if any(string in analyst4 for string in targetmedium):
                if totalprice4 <= medprice:
                    if cart4 == cartid4:
                        cart = cart4
                        JustDOit = SaleProcess(cart, cookie_value)                       
            #number5
            if any(text in analyst5 for text in targetknife):
                if totalprice5 <= 3000:
                    if cart5 == cartid5:
                        cart = cart5
                        JustDOit = SaleProcess(cart, cookie_value)

            if any(word in analyst5 for word in targetdoppler):
                if image5 in targetmg:
                    if totalprice5 <= superprice:               
                        if cart5 == cartid5:
                            cart = cart5
                            JustDOit = SaleProcess(cart, cookie_value)
                if image1 in targetm:
                    if totalprice5 <= gutdop:               
                        if cart5 == cartid5:
                            cart = cart5
                            JustDOit = SaleProcess(cart, cookie_value)

            if any(string in analyst5 for string in targetsuper):
                if totalprice5 <= superprice:
                    if cart5 == cartid5:
                        cart = cart5
                        JustDOit = SaleProcess(cart, cookie_value)   

            if any(string in analyst5 for string in targetmedium):
                if totalprice5 <= medprice:
                    if cart5 == cartid5:
                        cart = cart5
                        JustDOit = SaleProcess(cart, cookie_value)                        
            #number6
            if any(text in analyst6 for text in targetknife):
                if totalprice6 <= 3000:
                    if cart6 == cartid6:
                        cart = cart6
                        JustDOit = SaleProcess(cart, cookie_value)

            if any(word in analyst6 for word in targetdoppler):
                if image6 in targetmg:
                    if totalprice6 <= superprice:               
                        if cart6 == cartid6:
                            cart = cart6
                            JustDOit = SaleProcess(cart, cookie_value)
                if image1 in targetm:
                    if totalprice6 <= gutdop:               
                        if cart6 == cartid6:
                            cart = cart6
                            JustDOit = SaleProcess(cart, cookie_value)

            if any(string in analyst6 for string in targetsuper):
                if totalprice6 <= superprice:
                    if cart6 == cartid6:
                        cart = cart6
                        JustDOit = SaleProcess(cart, cookie_value)    

            if any(string in analyst6 for string in targetmedium):
                if totalprice6 <= medprice:
                    if cart6 == cartid6:
                        cart = cart6
                        JustDOit = SaleProcess(cart, cookie_value)                                                                      
            #number7
            if any(text in analyst7 for text in targetknife):
                if totalprice7 <= 3000:
                    if cart7 == cartid7:
                        cart = cart7
                        JustDOit = SaleProcess(cart, cookie_value)

            if any(word in analyst7 for word in targetdoppler):
                if image7 in targetmg:
                    if totalprice7 <= superprice:               
                        if cart7 == cartid7:
                            cart = cart7
                            JustDOit = SaleProcess(cart, cookie_value)
                if image1 in targetm:
                    if totalprice7 <= gutdop:               
                        if cart7 == cartid7:
                            cart = cart7
                            JustDOit = SaleProcess(cart, cookie_value)

            if any(string in analyst7 for string in targetsuper):
                if totalprice7 <= superprice:
                    if cart7 == cartid7:
                        cart = cart7
                        JustDOit = SaleProcess(cart, cookie_value)

            if any(string in analyst7 for string in targetmedium):
                if totalprice7 <= medprice:
                    if cart7 == cartid1:
                        cart = cart7
                        JustDOit = SaleProcess(cart, cookie_value)                        
            #number8
            if any(text in analyst8 for text in targetknife):
                if totalprice8 <= 3000:
                    if cart8 == cartid8:
                        cart = cart8
                        JustDOit = SaleProcess(cart, cookie_value)

            if any(word in analyst8 for word in targetdoppler):
                if image8 in targetmg:
                    if totalprice8 <= superprice:               
                        if cart8 == cartid8:
                            cart = cart8
                            JustDOit = SaleProcess(cart, cookie_value)
                if image1 in targetm:
                    if totalprice8 <= gutdop:               
                        if cart8 == cartid8:
                            cart = cart8
                            JustDOit = SaleProcess(cart, cookie_value)

            if any(string in analyst8 for string in targetsuper):
                if totalprice8 <= superprice:
                    if cart8 == cartid8:
                        cart = cart8
                        JustDOit = SaleProcess(cart, cookie_value)          

            if any(string in analyst8 for string in targetmedium):
                if totalprice8 <= medprice:
                    if cart8 == cartid8:
                        cart = cart8
                        JustDOit = SaleProcess(cart, cookie_value)
            #number9
            if any(text in analyst9 for text in targetknife):
                if totalprice9 <= 3000:
                    if cart9 == cartid9:
                        cart = cart9
                        JustDOit = SaleProcess(cart, cookie_value)

            if any(word in analyst9 for word in targetdoppler):
                if image9 in targetmg:
                    if totalprice9 <= superprice:               
                        if cart9 == cartid9:
                            cart = cart9
                            JustDOit = SaleProcess(cart, cookie_value)
                if image9 in targetm:
                    if totalprice9 <= gutdop:               
                        if cart9 == cartid9:
                            cart = cart9
                            JustDOit = SaleProcess(cart, cookie_value)

            if any(string in analyst9 for string in targetsuper):
                if totalprice9 <= superprice:
                    if cart9 == cartid9:
                        cart = cart9
                        JustDOit = SaleProcess(cart, cookie_value)          

            if any(string in analyst9 for string in targetmedium):
                if totalprice9 <= medprice:
                    if cart9 == cartid9:
                        cart = cart9
                        JustDOit = SaleProcess(cart, cookie_value)
            #number10
            if any(text in analyst10 for text in targetknife):
                if totalprice10 <= 3000:
                    if cart10 == cartid10:
                        cart = cart10
                        JustDOit = SaleProcess(cart, cookie_value)

            if any(word in analyst10 for word in targetdoppler):
                if image10 in targetmg:
                    if totalprice10 <= superprice:               
                        if cart10 == cartid10:
                            cart = cart10
                            JustDOit = SaleProcess(cart, cookie_value)
                if image1 in targetm:
                    if totalprice10 <= gutdop:               
                        if cart10 == cartid10:
                            cart = cart10
                            JustDOit = SaleProcess(cart, cookie_value)

            if any(string in analyst10 for string in targetsuper):
                if totalprice10 <= superprice:
                    if cart10 == cartid10:
                        cart = cart10
                        JustDOit = SaleProcess(cart, cookie_value)          

            if any(string in analyst10 for string in targetmedium):
                if totalprice10 <= medprice:
                    if cart10 == cartid10:
                        cart = cart10
                        JustDOit = SaleProcess(cart, cookie_value)  

            time.sleep(5)


if __name__ == "__main__":
    main()
\$\endgroup\$
11
+500
\$\begingroup\$

Unused variables

x, proc_c, start and JustDOit are set but never used, thus they are unneeded. For instance, write:

def main():
    while True:
        try:
            Scanner(cookie_value)
        except:
            print "= Sleeping for 1sec and trying again"
            time.sleep(1)

by the way, where does cookie_value come from? It is used all around your code but never set anywhere.

Flawed control flow

In your main function, what exceptions are you expecting exactly? Not even talking about the bare except that will handles exceptions that you are expecting as well as those you are not, but Scanner already performs exception management so why expect anything else to happen?

Also, you have a while True: in main and something that is pretty close in Scanner. This seems all too confusing. I’d rather have Scanner perform only one iteration over the IP addresses and let the while loop in the main.

Too much variables

The amount of variables in Scanner has already been discussed by @MaximGalushka. It’s approach to reduce the amount of copy pasting is pretty good, you can put all your second try .. except .. logic into his Parsed object constructor for maximum efficiency.

But you also happen to have all your targetXXX being the same without modifying them at any point. Why not use only one, then?

Flawed logic

You’re iterating over IP addresses but not using them to get any content. Instead, you’re always getting content from link. You probably want to define

LINK = "https://{}/testpage.html"

and then get the content using

requests.get(LINK.format(ipAddress))

You are also checking if cartX == cartidX way too many times. Check it once and if it fails there is no need to perform other checks.

Variable naming

Use proper case to differentiate between purpose of variables. TitleCase is usually for classes: use lowercase or lower_snake_case for function names. Use UPPERCASE for constants.

Proposed improvement

Using @MaximGalushka concept:

#!/usr/bin/env python
import requests
import socket
import random
import re
import time


LINK = 'https://{}/testpage.html'
IP_ADDRESSES = ['100.1.177.1', '100.1.177.2', '100.1.177.3']
SUPER_PRICE = 31867
MED_PRICE = 22992
GUTDOP = 12000

TARGETS = ["QHTxDZ7I56KU", "bXH5ApeO4", "tLu8JAllx8zJfA", "LwJ2UUGcVs_ns"]

class Parsed:
    def __init__(self, price, analist_id, image):
        cart, analyst = str(analyst_id).split(',')
        self.cart = cart[3:-1]
        self.analyst = analyst[2:-3]
        self.price = str(price).strip('[]').replace('\'', '')
        self.cart_id, total_price = self.price.split(',')
        self.total_price = float(total_price)
        self.image = str(image)[2:-3]

    def analyse(self):
        if self.cart == self.cart_id:
            self.analyse_price(TARGETS, 3000) # targetknife
            self.analyse_image(TARGETS, TARGETS, TARGETS, SUPER_PRICE, GUTDOP) # targetdoppler, targetmg, targetm
            self.analyse_price(TARGETS, SUPER_PRICE) # targetsuper
            self.analyse_price(TARGETS, MED_PRICE) # targetmedium

    def analyse_price(self, target, limit_price):
        if any(string in self.analyst for string in target):
            if self.total_price <= limit_price:
                self.process()

    def analyse_image(self, global_target, target_img_1, target_img_2, limit_price, gutdop):
        if any(word in self.analyst for word in global_target):
            if self.image in target_img_1:
                if self.total_price <= limit_price:
                    self.process()
            if self.image in target_img_2:
                if self.total_price <= gutdop:
                    self.process()

    def process(self):
        print "=========FOUND SOMETHING========="
        print self.cart


def parse_content():
    for ip in IP_ADDRESSES:
        address = LINK.format(ip)
        try:
            response = request.get(address)
        except requests.exceptions.RequestException:
            print "Can not fetch content from", address
            continue
        else:
            response_str = response.text

        analyst = re.findall("&item=(.*?)'>(.*?)<", response_str)
        price = re.findall('addToCart\((.*?)\)', response_str)    
        image = re.findall('image\/(.*?)\/256fx256f', response_str)

        try:
            parsed_objects = [Parsed(price[i], analyst[i], image[i]) for i in xrange(10)]
        except ValueError:
            print "Cannot parse content from", address
            continue

        for object in parsed_objects:
            object.analyse()

        time.sleep(5)

def main():
    while True:
        parse_content()

if __name__ == "__main__":
    main()
\$\endgroup\$
  • \$\begingroup\$ Selected best answer to the clear and complete suggestions offered. Thanks @mathias ettinger ! \$\endgroup\$ – Marie Anne Nov 2 '15 at 17:17
  • 1
    \$\begingroup\$ This answer is a winner of Best of Code Review 2015 — Night and Day category. Congratulations! \$\endgroup\$ – 200_success Jan 16 '16 at 0:47
  • \$\begingroup\$ Congratulations @mathias Ettinger ! \$\endgroup\$ – Marie Anne Jan 16 '16 at 21:53
  • \$\begingroup\$ @200_success As the category nominated both the question and the answer, is there a way for me to share a part of my prize with MarieAnne ? \$\endgroup\$ – 409_Conflict Jan 17 '16 at 10:48
  • \$\begingroup\$ Unfortunately, as MarieAnne has posted no answers on this site, there is no way to award a bounty. \$\endgroup\$ – 200_success Jan 17 '16 at 10:51
8
\$\begingroup\$

As I can see - looks like you have lots of different rules which should be applied on lots of different conditions.

Try to generify the approach you have. Here is possible sketch solution.

  • Define parsed object which will contain all values stored inside the object - those values which you have as cart1, analyst1, price1, image1 will belong to the single object Parsed

  • Parse the page and create list of Parsed objects - this is much less code and can be supported in a better way. You can do it in the simple loop instead of copy=paste same stuff with different indexes.

  • Create class Rule which will define the logic which should be applied to generic Parsed object.

  • Define specific set of rules - you don't have lots of variations as I can see here

  • Apply specific rules to specific objects using indexes in parsed list and rules list.

Hopefully this all makes sense to you. This will help you to separate objects construction from logic being applied on each object and maintain those separately.

Please, find very rough sketch (possibly not fully working - this is just give you an idea of what I mean).

class Parsed:
    def __init__(self, price, cart, image):
        self.price = price
        self.cart = cart
        self.image = image

class Rule:

    def __init__(self, from, where, what):
        self.from = from
        self.where = where
        self.what = what

    def apply(self, parsed):
        if any(word in from for word in where):
            # returns SaleProcess object
            return self.action(parsed)


parsed = []

# here is sketch for parsing your objects from the page
for i in range(1, 10):
    # put all values you need here - I put just 3 as example
    price, analystid, image = price[i:i+1], analystid[i:i+1], image[i:i+1]
    parsed.append(Parsed(price, analystid, image))

rules = []

rules.append(
    Rule(
        from=analyst,
        where=targetknife,
        # you can incapsulate logic to be applied as lambda here
        what=lambda parsed: 
            if parsed.image in targetmg:
                if parsed.totlpice <= superprice:               
                    if parsed.cart == parsed.cartid:
                        return SaleProcess(parsed.cart, cookie_value)
    )
)

# Now define all other rules based on parsed object values
# ....


# Now apply rules to specific objects you have parsed
# also you can use returned SaleProcess objects to do something to them straightaway
rule[0].apply(parsed[0]) 
rule[1].apply(parsed[1]) 
\$\endgroup\$
  • 3
    \$\begingroup\$ Good answer, just remember that a class that mainly contains some fields is usually implemented as a namedtuple for shortness \$\endgroup\$ – Caridorc Oct 26 '15 at 14:20
  • 1
    \$\begingroup\$ In fact Great answer, helped me rewrite my code better. Thank you \$\endgroup\$ – Marie Anne Nov 2 '15 at 17:16
3
\$\begingroup\$

As a general suggestions you may replace your groups of similarly named variables with a list and modify them all at once with a list comprehension.

And avoid bare except:, always specify the exception name to avoid swallowing bugs.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.