0

I have a script that imports a list from another file. The list contains an almost-dictionary, so I ran a loop through it to turn it into a dictionary, but I'm getting the following error:

Redefinition of unused productOrdersListPulled

What am I doing wrong?

My code is:

import ast
import re
from api import productOrdersListPulled

# This script removes some  characters from the messy dictionaries,
# Afterwards, they are a dictionary, recognizable by python

# regex to find unneeded data in the file
order_item = re.compile("<OrderProducts at 0x[\da-f]+, ({.*?})>", re.I)


def transformInDictionary(productOrdersListPulled):

    # opens up the file with the unstructured data, and use it as a iterator

        for line in productOrdersListPulled:
            productOrdersListWithLine = [ast.literal_eval(op) for op in re.findall(order_item, line)]
        return productOrdersListWithLine

transformInDictionary()

And the other file is:

import bigcommerce

# Script used to pull OrderProducts data from the Big Commerce API.

# login to bigcommerce api
api = bigcommerce.api.BigcommerceApi(host='*******.mybigcommerce.com', basic_auth=('**********', '*************'))


# create lists
def createLists():

    productOrdersList = list()
    ordersList = list()
    productsList = list()
    return productOrdersList, ordersList, productsList


# update range if orders ids goes higher than this
# try,except, pass added so the for loop keeps looping even if IDs are not found
# remember to update range (higest so far: 615982)
def pullOrdersProductsData(productOrdersList):
    for x in range(614530, 614535):
        try:
            productOrdersList.append(api.Orders.get(id="{}".format(x)).products())
        except:
            pass
    return productOrdersList


# loop over the list and get the orders with the IDs
def pullOrders(ordersList):
    #  remember to update range (higest so far: 615982)
    for x in range(614534, 614535):
        try:
            ordersList.append(api.Orders.get(id="{}".format(x)))
        except:
            pass
    return ordersList


def pullProductsData(productsList):

    # loop over the list and get the products with the Ids
    # dont forget to update range (highest so far 2000)
    for x in range(0, 5):
        try:
            productsList.append(api.Products.get(id="{}".format(x)))
        except:
            pass
    return productsList

productOrdersListPulled, ordersListPulled, productsListPulled = createLists()
pullOrdersProductsData(productOrdersListPulled)
pullOrders(ordersListPulled)
pullProductsData(productsListPulled)

2 Answers 2

1

You are importing productOrdersListPulled from api;

from api import productOrdersListPulled

and your function transformInDictionary takes an argument with the same name.

def transformInDictionary(productOrdersListPulled):

You should rename the argument of your function.

def transformInDictionary(lst):
    for line in lst:
        productOrdersListWithLine = [ast.literal_eval(op) for op in re.findall(order_item, line)]
    return productOrdersListWithLine

And call it as follows: (last line of your script)

transformInDictionary(productOrdersListPulled)
Sign up to request clarification or add additional context in comments.

3 Comments

How do I pass the productOrdersListPulled to transformInDiciontary if I use a different name?
Since you seem to want to use it as a global, you wouldn't as you don't need to.
that worked, thanks. now i have to fix the re module, that wont work with lists, only string hehe
0

First , i think your parameter name is too conflict .

Moreover , your method signature probably incorrect ? transformInDictionary take one argument , but you call with zero argument

I think this may solve your problem ?

from api import productOrdersListPulled as products #Give alias to productOrdersListPulled
def transformInDiction():
    """
    Change to zero argument
    """
    for line in products :
        product= [ast.literal_eval(op) for op in re.findall(order_item, line)]
    return product
transformInDiction()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.