Skip to content

Feature load queue and length of queue #104

Merged
merged 8 commits into from
Oct 30, 2020

Conversation

benne238
Copy link
Collaborator

No description provided.

@campb303
Copy link
Collaborator

This is functional but it doesn't follow the DRY (don't repeat yourself) principle. Rather than copy code from the existing getQueues() function, we can extract functionality into different functions and recombine them for the desired output.

If we analyze the current getQueeus() function we can see it does the following:

  1. Loop through all files in the queueDirectory
  2. Check if file is a directory (directories are queues)
  3. Check if file is not in queuesToIgnore (some queues shouldn't be loaded)
  4. Creates a list of queues for valid files
def getQueues() -> list:
	"""Return a list of Queues for each queue.

	Returns:
		list: list of Queues for each queue.
	"""
	queues = []

	for file in os.listdir(queueDirectory):
		currentFile = queueDirectory + "/" + file
		isDirectory = os.path.isdir(currentFile)
		isValid = file not in queuesToIgnore

		if isDirectory and isValid:
			queues.append(Queue(file))

	return queues

During queue creation, Queue.__getItems() does the following:

  1. Loops through all of the files in the queue's directory
  2. Checks if the file is a file (as opposed to a directory, symbolic link etc.)
  3. Checks if the file's name contains only 1 to 3 numeric characters (to avoid lock files that end in .lck)
  4. Creates an Item from the file and appends it to the Queue's item list
def __getItems(self) -> list:
	"""Returns a list of items for this Queue

	Returns:
		list: a list of items for this Queue
	"""
	items = []

	for item in os.listdir(self.__directory):
		itemPath = self.__directory + "/" + item
		
		isFile = True if os.path.isfile(itemPath) else False
		
		itemPattern = re.compile("^[0123456789]{1,3}$")
		isItem = True if itemPattern.match(item) else False

		if isFile and isItem:
			items.append( Item(self.name, item) )

	return items

We need to break these two functions apart to allow for the new sets of data we need to return. Those sets of data are:

Data Types
Available queues and their number of items. A list of dictionaries where the dictionaries have keys of name for the name of the queue and number_of_items for the number of items in the queue.
Loaded queues and the header information from their items. A list of Queues with every field except content.
An entire item. An instance of an Item with all fields including content.

This would start with extracting functionality like building a list of queues, and validating Item file names into separate functions and recombining those functions to get the desired outputs.

Copy link
Collaborator

@campb303 campb303 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Common functionality needs extracted into callable functions rather than repeated.

@campb303 campb303 merged commit c23c678 into staging Oct 30, 2020
@campb303 campb303 deleted the Feature-Load-Queue-and-Length-of_Queue branch October 30, 2020 15:45
Sign in to join this conversation on GitHub.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants