473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Searching for some kind of data type

Hi,
for an FTP server I wrote I'd need to group the FTP commands in one
table that defines the command itself, the syntax string, required
permission, whether it requires authorization, whether it takes
argument and whether there's a need to validate the path from the
argument.
The more obvious way I found to do that is something like this:

class CommandProperty:
def __init__(self, perm, auth_needed, arg_needed, check_path,
syntax):
self.perm = perm
self.auth_needed = auth_needed
self.arg_needed = arg_needed
self.check_path = check_path
self.syntax = syntax

ftp_cmds = {
"ABOR" : CommandProperty(perm=None, auth_needed=True,
arg_needed=False, check_path=False, syntax="ABOR (abort transfer)."),
"APPE" : CommandProperty(perm='a', auth_needed=True,
arg_needed=True, check_path=True, syntax="APPE <SPfile-name
(append data to an existent file)."),
"CDUP" : CommandProperty(perm='e',
auth_needed=True,arg_needed=False, check_path=False, syntax="CDUP (go
to parentdirectory)."),
...
...
...
}

....but I find it somewhat redundant and... "ugly".
I was wondering if there was some kind of data type which could better
fit such purpose or if someone could suggest me some other approach to
do this same thing. Maybe using a dictionary is not the better choice
here.
Thanks in advance
--- Giampaolo
http://code.google.com/p/pyftpdlib/
Aug 2 '08 #1
4 1300
Giampaolo Rodola' wrote:
Hi,
for an FTP server I wrote I'd need to group the FTP commands in one
table that defines the command itself, the syntax string, required
permission, whether it requires authorization, whether it takes
argument and whether there's a need to validate the path from the
argument.
The more obvious way I found to do that is something like this:

class CommandProperty:
def __init__(self, perm, auth_needed, arg_needed, check_path,
syntax):
self.perm = perm
self.auth_needed = auth_needed
self.arg_needed = arg_needed
self.check_path = check_path
self.syntax = syntax

ftp_cmds = {
"ABOR" : CommandProperty(perm=None, auth_needed=True,
arg_needed=False, check_path=False, syntax="ABOR (abort transfer)."),
"APPE" : CommandProperty(perm='a', auth_needed=True,
arg_needed=True, check_path=True, syntax="APPE <SPfile-name
(append data to an existent file)."),
"CDUP" : CommandProperty(perm='e',
auth_needed=True,arg_needed=False, check_path=False, syntax="CDUP (go
to parentdirectory)."),
...
...
...
}

...but I find it somewhat redundant and... "ugly".
I was wondering if there was some kind of data type which could better
fit such purpose or if someone could suggest me some other approach to
do this same thing. Maybe using a dictionary is not the better choice
here.
Thanks in advance
--- Giampaolo
http://code.google.com/p/pyftpdlib/

Seems completely reasonable to me. You might just consider using keyword
arguments in the __init__ method and eliminate the dictionary altogether.

Not tested, but you will get the idea:

class CommandProperty:
def __init__(self, perm = perm, auth_needed = True, arg_needed = True,
check_path = False, syntax = syntax):

self.perm = perm
self.auth_needed = auth_needed
self.arg_needed = arg_needed
self.check_path = check_path
self.syntax = syntax

ftpCommands = dict(
ABOR = CommandProperty(perm = None, arg_needed = False,
syntax="ABOR (abort transfer)."),
APPE = CommandProperty(perm = 'a', check_path=True,
syntax = "APPE <SPfile-name (append data to" \
"an existent file)."),
CDUP = CommandProperty(perm= 'e', arg_needed = False,
syntax="CDUP (goto parentdirectory)."),
....
....
....
)
IMHO this is a "little" easier to manage because you can take advantage of the
default values for keyword arguments to eliminate some of the arguments.

Hope this helps,
Larry
Aug 2 '08 #2
Larry Bates wrote:
Giampaolo Rodola' wrote:
>Hi,
for an FTP server I wrote I'd need to group the FTP commands in one
table that defines the command itself, the syntax string, required
permission, whether it requires authorization, whether it takes
argument and whether there's a need to validate the path from the
argument.
The more obvious way I found to do that is something like this:

class CommandProperty:
def __init__(self, perm, auth_needed, arg_needed, check_path,
syntax):
self.perm = perm
self.auth_needed = auth_needed
self.arg_needed = arg_needed
self.check_path = check_path
self.syntax = syntax

ftp_cmds = {
"ABOR" : CommandProperty(perm=None, auth_needed=True,
arg_needed=False, check_path=False, syntax="ABOR (abort transfer)."),
"APPE" : CommandProperty(perm='a', auth_needed=True,
arg_needed=True, check_path=True, syntax="APPE <SPfile-name
(append data to an existent file)."),
"CDUP" : CommandProperty(perm='e',
auth_needed=True,arg_needed=False, check_path=False, syntax="CDUP (go
to parentdirectory)."),
...
...
...
}

...but I find it somewhat redundant and... "ugly".
I was wondering if there was some kind of data type which could better
fit such purpose or if someone could suggest me some other approach to
do this same thing. Maybe using a dictionary is not the better choice
here.
Thanks in advance
--- Giampaolo
http://code.google.com/p/pyftpdlib/


Seems completely reasonable to me. You might just consider using
keyword arguments in the __init__ method and eliminate the dictionary
altogether.

Not tested, but you will get the idea:

class CommandProperty:
def __init__(self, perm = perm, auth_needed = True, arg_needed =
True,
check_path = False, syntax = syntax):

self.perm = perm
self.auth_needed = auth_needed
self.arg_needed = arg_needed
self.check_path = check_path
self.syntax = syntax

ftpCommands = dict(
ABOR = CommandProperty(perm = None, arg_needed = False,
syntax="ABOR (abort transfer)."),
APPE = CommandProperty(perm = 'a', check_path=True,
syntax = "APPE <SPfile-name (append data to" \
"an existent file)."),
CDUP = CommandProperty(perm= 'e', arg_needed = False,
syntax="CDUP (goto parentdirectory)."),
...
...
...
)
How does this strike you? With care, the comment and the table could
be kept aligned and nicely readable

cmd_data = dict(
# cmd = (perm, auth, arg, path, syntax),
ABOR = (None, False, False, False, "ABOR (abort transfer)."),
APPE = (None, False, False, True, "APPE <SPfile-name (append data to"),
...
]

ftpCommands = {}
for cmd,args in cmd_data.iteritems():
ftpCommands[cmd] = CommandProperty(*args)

Gary Herron
>

IMHO this is a "little" easier to manage because you can take
advantage of the default values for keyword arguments to eliminate
some of the arguments.

Hope this helps,
Larry
--
http://mail.python.org/mailman/listinfo/python-list
Aug 2 '08 #3
On 2 Ago, 18:18, Gary Herron <gher...@islandtraining.comwrote:
Larry Bates wrote:
Giampaolo Rodola' wrote:
Hi,
for an FTP server I wrote I'd need to group the FTP commands in one
table that defines the command itself, the syntax string, required
permission, whether it requires authorization, whether it takes
argument and whether there's a need to validate the path from the
argument.
The more obvious way I found to do that is something like this:
class CommandProperty:
* * def __init__(self, perm, auth_needed, arg_needed, check_path,
syntax):
* * * * self.perm = perm
* * * * self.auth_needed = auth_needed
* * * * self.arg_needed = arg_needed
* * * * self.check_path = check_path
* * * * self.syntax = syntax
ftp_cmds = {
* * "ABOR" : CommandProperty(perm=None, auth_needed=True,
arg_needed=False, check_path=False, syntax="ABOR (abort transfer)."),
* * "APPE" : CommandProperty(perm='a', *auth_needed=True,
arg_needed=True, *check_path=True, *syntax="APPE <SPfile-name
(append data to an existent file)."),
* * "CDUP" : CommandProperty(perm='e',
auth_needed=True,arg_needed=False, check_path=False, syntax="CDUP (go
to parentdirectory)."),
* * ...
* * ...
* * ...
* * }
...but I find it somewhat redundant and... "ugly".
I was wondering if there was some kind of data type which could better
fit such purpose or if someone could suggest me some other approach to
do this same thing. Maybe using a dictionary is not the better choice
here.
Thanks in advance
--- Giampaolo
http://code.google.com/p/pyftpdlib/
Seems completely reasonable to me. *You might just consider using
keyword arguments in the __init__ method and eliminate the dictionary
altogether.
Not tested, but you will get the idea:
class CommandProperty:
* * def __init__(self, perm = perm, auth_needed = True, arg_needed =
True,
* * * * * * * * *check_path = False, syntax = syntax):
* * * * self.perm = perm
* * * * self.auth_needed = auth_needed
* * * * self.arg_needed = arg_needed
* * * * self.check_path = check_path
* * * * self.syntax = syntax
ftpCommands = dict(
* * ABOR = CommandProperty(perm = None, arg_needed = False,
* * * * * * *syntax="ABOR (abort transfer)."),
* * APPE = CommandProperty(perm = 'a', *check_path=True,
* * * * * * *syntax = "APPE <SPfile-name (append datato" \
* * * * * * * * * * * "an existent file)."),
* * CDUP = CommandProperty(perm= 'e', arg_needed = False,
* * * * * * *syntax="CDUP (goto parentdirectory)."),
...
...
...
* * )

How does this strike you? * *With care, the comment and the table could
be kept aligned and nicely readable

cmd_data = dict(
*# cmd = (perm, auth, * arg, path, * syntax),
*ABOR = (None, False, False, False, "ABOR (abort transfer)."),
*APPE *= (None, False, False, True, *"APPE <SPfile-name (appenddata to"),
*...
]

ftpCommands = {}
for cmd,args in cmd_data.iteritems():
* * ftpCommands[cmd] = CommandProperty(*args)

Gary Herron


IMHO this is a "little" easier to manage because you can take
advantage of the default values for keyword arguments to eliminate
some of the arguments.
Hope this helps,
Larry
--
http://mail.python.org/mailman/listinfo/python-list- Nascondi testo citato

- Mostra testo citato -- Nascondi testo citato

- Mostra testo citato -
Thanks, I didnt' know dict() could be used with =.
I think I'm going to use this solution.
--- Giampaolo
http://code.google.com/p/pyftpdlib/
Aug 2 '08 #4
On Aug 2, 1:12*pm, "Giampaolo Rodola'" <gne...@gmail.comwrote:
Hi,
for an FTP server I wrote I'd need to group the FTP commands in one
table that defines the command itself, the syntax string, required
permission, whether it requires authorization, whether it takes
argument and whether there's a need to validate the path from the
argument.
The more obvious way I found to do that is something like this:

class CommandProperty:
* * def __init__(self, perm, auth_needed, arg_needed, check_path,
syntax):
* * * * self.perm = perm
* * * * self.auth_needed = auth_needed
* * * * self.arg_needed = arg_needed
* * * * self.check_path = check_path
* * * * self.syntax = syntax

ftp_cmds = {
* * "ABOR" : CommandProperty(perm=None, auth_needed=True,
arg_needed=False, check_path=False, syntax="ABOR (abort transfer)."),
* * "APPE" : CommandProperty(perm='a', *auth_needed=True,
arg_needed=True, *check_path=True, *syntax="APPE <SPfile-name
(append data to an existent file)."),
* * "CDUP" : CommandProperty(perm='e',
auth_needed=True,arg_needed=False, check_path=False, syntax="CDUP(go
to parentdirectory)."),
* * ...
* * ...
* * ...
* * }

...but I find it somewhat redundant and... "ugly".
I was wondering if there was some kind of data type which could better
fit such purpose or if someone could suggest me some other approach to
do this same thing. Maybe using a dictionary is not the better choice
here.
How about something like this? The idea is to summarize the command
table, then construct the ftp command dictionary programatically from
it.

cmd_table = dict(
ABOR='-AR- (abort transfer)',
APPE='aARP <SPfile-name (append data to a file)',
CDUP='eA-- (go to parent directory)',
...
)

The first block of four characters give the options: permission,
whether authentication is needed, whether the argument is required,
and whether the path needs checking. Permission is the permission
letter or - (for none), A or - for authentication, R or - for argument
required, and - or P for path required. Then the help text follows
(with the command name omitted since it's redundant).

Turning these into CommandProperty's is straightforward (untested
code):

ftp_commands = {}
for cmd, descriptor in cmd_table.iteritems():
options, help = descriptor.split(' ', 1)
options = [None if opt == '-' else opt for opt in options]
perm, auth, arg, path = options
assert perm is None or perm in 'ae...'
assert auth is None or auth == 'A'
assert arg is None or arg == 'R'
assert path is None or path == 'P'
ftp_commands[cmd] = CommandProperty(perm=perm, auth_required=auth,
arg_required=arg, check_path=path, syntax = '%s %s' % (cmd, help))

--
Paul Hankin
Aug 2 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Joh | last post by:
Hello, (sorry long) i think i have missed something in the code below, i would like to design some kind of detector with python, but i feel totally in a no way now and need some advices to...
13
by: gtux | last post by:
Hi everybody: I'm new in Javascript, I found some code and there is this: var fruit = { 'apple' : { 'weight' : 10, 'cost' : 9}, 'peach' : { 'weight' : 19, 'cost' : 10} }
2
by: Matias | last post by:
Hi: I need some help, about How can I define a GLOBAL Data type in ASP.net proyect. thanks
10
by: mattmerc | last post by:
Hi all, I posted this about a week back with no response so I want to try again. I have a weird questions in regards to working with sql image data types. I know how to take the image type and...
5
by: rAinDeEr | last post by:
Hi, I have a web application with a table to store terms and conditions of a Company. This may some times run into many pages and some times it may be just a few sentences. It is a character...
1
by: neeraj | last post by:
Hi All Can any give me the code for convert "DataColumn" data type of "DataTable". Even if data table already populated (have data) Actually I am creating one search module which searches the...
35
by: dtschoepe | last post by:
Greetings. I am working on an assignment and can't seem to get the right concept for something I'm attempting to do with enum data types. I have defined the following in my code: enum color...
5
by: isoquin | last post by:
The below code throws the error: "Data type mismatch in criteria expression" Searching online didn't seem to offer me a solution. Public Sub update_by_EDD(myDate As Date, myMR As String) ...
0
by: Adam Chapman | last post by:
Hi, Im completely new to c++ and I am trying to interface some c++ source code with matlab. Mt question is: I there a way to find the data type of a variable other than searching through the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.