Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8286759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:51:55+00:00 2026-06-08T11:51:55+00:00

I am using Python2.7 with the SimPy module, first time posting here. I am

  • 0

I am using Python2.7 with the SimPy module, first time posting here.
I am just learning both of them so I hope I explain this correctly.
The aim of my program:
Create a Demand object and generate a number weekly.
Store it in a list.
Create a Supply object and generate a number weekly, based on the number created by the demand object.
I seem to be able to create my 52 numbers, and append them to a list, but I cant succesfully get the Supply object to read through the list.
My code is as follows:

from SimPy.Simulation import *
import pylab as pyl
from random import Random
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
# Model components
runLength = 51

## Lists for examination
D1Vals = []
S1Vals = []

.... other code lines

class Demander(Process):

# This object creates the demand, and stores the values in the 'D1Vals' list above

def weeklyDemand(self):  # Demand Weekly
    while True:
            lead = 1.0      # time between demand requests
            demand = random.triangular(20,110,370)  # amount demanded each increment
            #yield put, self, orderBook, delivery
            print('Week'+'%6.0f: Need %6.0f units: Total Demand = %6.0f' %
                    (now(), demand, orderBook.amount))
            yield hold, self, lead
            yield put, self, orderBook, demand
            D1Vals.append(demand)

# This object is trying to read each value iteratively in D1Vals, 
  and create a supply value and store in a list 'S1Vals'

class Supplier(Process):

def supply_rate(self):
    lead = 1.0
    for x in D1Vals:
            supply = random.triangular(x - 30, x , x + 30)               
            yield put, self, stocked, supply
            print('Week'+'%6.0f: Gave %6.0f units: Inv. Created = %6.0f' %
                    (now(), supply,stocked.amount))
            yield hold, self, lead
            S1Vals.append(stocked.amount)

..... other misc coding .....

# Model
demand_1 = Demander()
activate(demand_1, demand_1.weeklyDemand())
supply_1 = Supplier()
activate(supply_1, supply_1.supply_rate())
simulate(until=runLength)

When I run my program, it creates my demand and outputs the weekly and cumulative values to the console, it also prints the D1Vals list for me to see that it is not empty.

Can anyone please guide me to the correct path for succesfully reading the list from the Supplier Object and function.
Thanks and please excuse my obvious ‘fresh’ outlook on python 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:51:56+00:00Added an answer on June 8, 2026 at 11:51 am

    D1Vals and S1Vals are defined at the module scope;
    You should be able to write expressions like
    x=S1Vals[-7:] anywhere in that module without a problem.

    This works for accessing the value of and mutating the value of these variables,
    so that

    def append_supply( s ):
        S1Vals.append(s)
    

    should work.

    To assign to them however, you need to declare them as global

    def erase_supply():
        '''Clear the list of supply values'''
        global S1Vals
        S1Vals = []
    

    If the global S1Vals line is omitted, the result will be that the function-local
    variable S1Vals is defined by the assignment statement, shadowing the module-level
    variable with the same name.

    A way to avoid using the global statement is to use the actual module name
    to refer to these variables. I’ll assume that your code is defined in
    SupplyAndDemandModel.py.

    At the top of this file you can put

    import SupplyAndDemandModel
    

    and then refer to those module-scoped variables using the module name:

    SupplyAndDemandModel.S1Vals = []
    

    This provides a way of unambiguously indicating that you are accessing/modifying a module-level variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a sample run of simple json using python2.4 version >>> >>> orig='{key1:Val,
Python novice here. I am using python2.7.2 on Windows7. I have installed the PyWin32
I have been learning Python2.7 for a little bit now. I'm using Windows 7
I've been using the SimPy module for Python - that does Discrete Event Simulations.
Using Python2.4 I want to capture output from a mysql command. One caveat is
I'm trying to learn GUI programming using python2 and GTKBuilder, but I get a
I am running below code as root and using python2.6.1, platform is linux >>>
I'm using the Python2.7 runtime with threadsafe set to false in the manifest. Am
Using Python's csv module, is it possible to read an entire, large, csv file
Im using Pylons in combination with WMI module to do some basic system monitoring

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.